在java中添加一个元素到int []数组[复制](add an element to int [] array in java [duplicate])

这个问题在这里已经有了答案:

如何将新元素添加到数组中? 16个答案

想要添加或追加元素到现有的数组

int[] series = {4,2};

现在我想用我发送的新值动态更新系列。

就像我发送3个更新系列一样int[] series = {4,2,3};

再次如果我发送4更新系列作为int[] series = {4,2,3,4};

再次如果我发送1更新系列作为int[] series = {4,2,3,4,1}; 等等

怎么做????

我在其他函数中每5分钟生成一个整数,并想发送以更新int[] series数组。

This question already has an answer here:

How to add new elements to an array? 17 answers

Want to add or append elements to existing array

int[] series = {4,2};

now i want to update the series dynamically with new values i send..

like if i send 3 update series as int[] series = {4,2,3};

again if i send 4 update series as int[] series = {4,2,3,4};

again if i send 1 update series as int[] series = {4,2,3,4,1}; so on

How to do it????

I generate an integer every 5 minutes in some other function and want to send to update the int[] series array..

最满意答案

java中的数组长度是不可变的。 这意味着一旦你创建了一个数组,你就不能改变它的大小。 如果使用2个元素初始化它,则其长度为2.然而,您可以使用不同的集合。

List<Integer> myList = new ArrayList<Integer>(); myList.add(5); myList.add(7);

使用包装器方法

public void addMember(Integer x) { myList.add(x); };

The length of an array is immutable in java. This means you can't change the size of an array once you have created it. If you initialised it with 2 elements, its length is 2. You can however use a different collection.

List<Integer> myList = new ArrayList<Integer>(); myList.add(5); myList.add(7);

And with a wrapper method

public void addMember(Integer x) { myList.add(x); };在java中添加一个元素到int []数组[复制](add an element to int [] array in java [duplicate])

这个问题在这里已经有了答案:

如何将新元素添加到数组中? 16个答案

想要添加或追加元素到现有的数组

int[] series = {4,2};

现在我想用我发送的新值动态更新系列。

就像我发送3个更新系列一样int[] series = {4,2,3};

再次如果我发送4更新系列作为int[] series = {4,2,3,4};

再次如果我发送1更新系列作为int[] series = {4,2,3,4,1}; 等等

怎么做????

我在其他函数中每5分钟生成一个整数,并想发送以更新int[] series数组。

This question already has an answer here:

How to add new elements to an array? 17 answers

Want to add or append elements to existing array

int[] series = {4,2};

now i want to update the series dynamically with new values i send..

like if i send 3 update series as int[] series = {4,2,3};

again if i send 4 update series as int[] series = {4,2,3,4};

again if i send 1 update series as int[] series = {4,2,3,4,1}; so on

How to do it????

I generate an integer every 5 minutes in some other function and want to send to update the int[] series array..

最满意答案

java中的数组长度是不可变的。 这意味着一旦你创建了一个数组,你就不能改变它的大小。 如果使用2个元素初始化它,则其长度为2.然而,您可以使用不同的集合。

List<Integer> myList = new ArrayList<Integer>(); myList.add(5); myList.add(7);

使用包装器方法

public void addMember(Integer x) { myList.add(x); };

The length of an array is immutable in java. This means you can't change the size of an array once you have created it. If you initialised it with 2 elements, its length is 2. You can however use a different collection.

List<Integer> myList = new ArrayList<Integer>(); myList.add(5); myList.add(7);

And with a wrapper method

public void addMember(Integer x) { myList.add(x); };