如何在Java中将元素附加到ArrayList的末尾?

34

我想知道在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是一种生成随机字符串的方法。

我想要始终将随机字符串附加到ArrayList的末尾,就像一个堆栈一样(因此名称为“push”)。


4
使用stringList.add,它会被“追加”到末尾。 - Maroun
4
你认为这句代码 - ArrayList.add(random); 会做什么?它将添加到哪个arraylist中? - Rohit Jain
1
如果你想要一个像栈一样工作的东西,为什么不使用 Stack? - Anthony Grist
1
@AnthonyGrist 他正在尝试实现一个由ArrayList支持的Stack - Maroun
@RohitJain 我现在明白这是错误的了,我是编程新手,认为必须在ArrayList上使用方法“add”,因为该方法属于该类,然后我认为如果你写“random”,它会将字符串“random”附加到ArrayList上,但是当然,这没有多少意义,因为计算机怎么知道要将其添加到“stringList”中呢? - Nora
显示剩余2条评论
4个回答

59

以下是语法以及其他可能有用的方法:

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

3

我遇到了类似的问题,只需将数组的结尾传递给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);
    }

}

2

我知道这是一个老问题,但我想提供自己的答案。如果你“真的”想在列表末尾添加内容而不是使用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));

这是在列表末尾添加项目的“紧凑”方式。下面是更安全的方法,包括空值检查等。

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

这里有另一种将项目添加到列表末尾的方法,愉快编码 :)

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

}

我认为这个例子会帮助你找到更好的解决方案。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接