Java - 从列表中检索值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14028171/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:48:33  来源:igfitidea点击:

Java - Retrieve values from List

javastringlistarraylist

提问by user1926452

I added 3 strings into a List (Should I call this ArrayList ?) And don't know how to retrieve.

我在 List 中添加了 3 个字符串(我应该称它为 ArrayList 吗?)但不知道如何检索。

      String f1;
      String f2;
      String f3;
      f1="1";
      f2="2";
      f3="3";
      List<String[]> list = new ArrayList<String[]>();
      String[] e = new String[] {f1,f2,f3};
       list.add(e);
       System.out.println(list.get(0,1));
      for (int inT=0;inT<=list.size();inT++)
      {
          System.out.println(list.get(inT));
      }

回答by Jayamohan

If you just want to add some items and retrive them you dont need a separate arrays to be stored in the ArrayList. List means List of arrays and not List of Strings.

如果您只想添加一些项目并检索它们,则不需要将单独的数组存储在 ArrayList 中。列表表示数组列表而不是字符串列表。

You should be doing this,

你应该这样做,

String f1;
String f2;
String f3;
f1 = "1";
f2 = "2";
f3 = "3";
List<String> list = new ArrayList<String>();
list.add(f1);
list.add(f2);
list.add(f3);

for (String listItem : list) {
    System.out.println(listItem);
}