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

查看:93
本文介绍了在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[] series 数组..

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天全站免登陆