【问题标题】:How to append elements at the end of ArrayList in Java?如何在Java中的ArrayList末尾附加元素?
【发布时间】:2014-05-01 21:01:30
【问题描述】:

我想知道,如何在 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”)。

非常感谢您抽出宝贵时间!

【问题讨论】:

  • 使用stringList.add。它被“附加”到最后..
  • 你认为这 - ArrayList.add(random); 会做什么?它将添加到哪个数组列表?
  • 如果您想要像堆栈一样工作的东西,为什么不使用堆栈?
  • @AnthonyGrist 他正在尝试实现由ArrayList 支持的Stack
  • @RohitJain 我现在知道这是错误的,我是编程新手,并认为您必须在 ArrayList 上使用“add”方法,因为该方法属于该类,然后我以为如果你写“random”,它会将字符串“random”附加到ArrayList,但当然,这没什么意义,因为计算机怎么知道将它添加到“stringList”,对吧?

标签: java arraylist append


【解决方案1】:

以下是语法,以及您可能会发现有用的其他一些方法:

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

【讨论】:

    【解决方案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;
        }
    

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

    【讨论】:

      【解决方案3】:
      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);
          }
      
      }
      

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 2012-09-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-05-28
          • 1970-01-01
          • 1970-01-01
          • 2023-03-07
          • 1970-01-01
          相关资源
          最近更新 更多