在java中为int []数组添加一个元素 [英] add an element to int [] array in java

查看:407
本文介绍了在java中为int []数组添加一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想要向现有数组添加或附加元素

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..

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

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

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

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

如果我发送1个更新系列为 int [] series = {4,2,3,4,1}; 那么

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

怎么做????

我在其他一些函数中每隔5分钟生成一个整数并想要发送更新 int []系列数组..

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

推荐答案

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

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);

使用包装方法

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

这篇关于在java中为int []数组添加一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆