如何在Java中的ArrayList末尾追加元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22610526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to append elements at the end of ArrayList in Java?
提问by Nora
I am wondering, how do I append an element to the end of an ArrayList in Java? Here is the code I have so far:
我想知道,如何在 Java 中将元素附加到 ArrayList 的末尾?这是我到目前为止的代码:
public class Stack {
private ArrayList<String> stringList = new ArrayList<String>();
RandomStringGenerator rsg = new RandomStringGenerator();
private void push(){
String random = rsg.randomStringGenerator();
ArrayList.add(random);
}
}
"randomStringGenerator" is a method which generates a random String.
“randomStringGenerator”是一种生成随机字符串的方法。
I basically want to always append the random string at the end of the ArrayList, much like a stack (Hence the name "push").
我基本上想总是在 ArrayList 的末尾附加随机字符串,就像堆栈一样(因此名称为“push”)。
Thank you so much for your time!
非常感谢您的参与!
采纳答案by Edwin Torres D.Eng.
Here is the syntax, along with some other methods you might find useful:
这是语法,以及您可能会发现有用的其他一些方法:
//add to the end of the list
stringList.add(random);
//add to the beginning of the list
stringList.add(0, random);
//replace the element at index 4 with random
stringList.set(4, random);
//remove the element at index 5
stringList.remove(5);
//remove all elements from the list
stringList.clear();
回答by SamzSakerz
I know this is an old question, but I wanted to make an answer of my own. here is another way to do this if you "really" want to add to the end of the list instead of using list.add(str)
you can do it this way, but I don't recommend.
我知道这是一个老问题,但我想自己做一个答案。如果您“真的”想添加到列表的末尾而不是使用list.add(str)
您可以这样做,这是另一种方法,但我不建议这样做。
String[] items = new String[]{"Hello", "World"};
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list, items);
int endOfList = list.size();
list.add(endOfList, "This goes end of list");
System.out.println(Collections.singletonList(list));
this is the 'Compact' way of adding the item to the end of list. here is a safer way to do this, with null checking and more.
这是将项目添加到列表末尾的“紧凑”方式。这是一种更安全的方法,包括空值检查等。
String[] items = new String[]{"Hello", "World"};
ArrayList<String> list = new ArrayList<>();
Collections.addAll(list, items);
addEndOfList(list, "Safer way");
System.out.println(Collections.singletonList(list));
private static void addEndOfList(List<String> list, String item){
try{
list.add(getEndOfList(list), item);
} catch (IndexOutOfBoundsException e){
System.out.println(e.toString());
}
}
private static int getEndOfList(List<String> list){
if(list != null) {
return list.size();
}
return -1;
}
Heres another way to add items to the end of list, happy coding :)
这是将项目添加到列表末尾的另一种方法,快乐编码:)
回答by KEERTHAN SHETTY
import java.util.*;
public class matrixcecil {
public static void main(String args[]){
List<Integer> k1=new ArrayList<Integer>(10);
k1.add(23);
k1.add(10);
k1.add(20);
k1.add(24);
int i=0;
while(k1.size()<10){
if(i==(k1.get(k1.size()-1))){
}
i=k1.get(k1.size()-1);
k1.add(30);
i++;
break;
}
System.out.println(k1);
}
}
I think this example will help you for better solution.
我认为这个例子会帮助你更好地解决问题。
回答by Ambrown
I ran into a similar problem and just passed the end of the array to the ArrayList.add()
index param like so:
我遇到了类似的问题,只是将数组的末尾传递给ArrayList.add()
索引参数,如下所示:
public class Stack {
private ArrayList<String> stringList = new ArrayList<String>();
RandomStringGenerator rsg = new RandomStringGenerator();
private void push(){
String random = rsg.randomStringGenerator();
stringList.add(stringList.size(), random);
}
}