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

编程入门 行业动态 更新时间:2024-10-06 18:31:54
在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); };

更多推荐

本文发布于:2023-08-07 16:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465219.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   元素   int   java   duplicate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!