Java 如何在数组列表中存储字符串数组。我们可以使用数组列表的数组吗?如果是,那么如何?

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

how to store a array of String in Array List. can we use array of an Array List. if yes then how?

java

提问by sam

is it possible to run the following code with logic in 6th line ?

是否可以在第 6 行中使用逻辑运行以下代码?

public class arraylist{
    public static void main(String args{}){
        String s[]={"Sam","Tom","Jerry"};
        ArrayList al=new ArrayList();
        al.add(s);//i want this type of logic so i can add the elements of string once.is it possible?
    }

    Iterator it=al1.iterator();
    while(it.hasNext())
    {

        String element=String.valueOf(it.next());
        System.out.print("Element"+element);
    }
}

回答by Martin Seeler

Try the following:

请尝试以下操作:

ArrayList<String> al = new ArrayList<String>(Arrays.asList(s));

回答by Gyanendra Dwivedi

Change al.add(s);by al.addAll(Arrays.asList(s));and you should be all set.

改变al.add(s);al.addAll(Arrays.asList(s));,你应该准备就绪。

回答by JNL

You have the answer in your question.

你在你的问题中有答案。

When you say you want to convert array asList

当你说你想将数组转换为List

回答by Prabhaker A

al.add(s);//i want this type of logic so i can add the elements of string once.is it possible ?
Yes. It is possible.You can add any object to ArrayListincluding array object.
But while iterating the ArrayListobject you will get an array element by calling it.next().So output will be String representation of array objectnot the array elements
So try this

al.add(s);//i want this type of logic so i can add the elements of string once.is it possible ?
是的。这是可能的。您可以将任何对象添加到ArrayList包含数组对象中。
但是在迭代ArrayList对象时,您将通过调用获得一个数组元素it.next()。所以输出将String representation of array object不是数组元素
所以试试这个

String s[]={"Sam","Tom","Jerry"};
ArrayList<String> al=Arrays.asList(s);        
Iterator it=al.iterator();
while(it.hasNext())
{
    String element=String.valueOf(it.next());
    System.out.print("Element"+element);
}

回答by Tim

As many have already suggested, use Arrays.asList. But before the code would work, you would still need to fix the formatting as you have code outside the main method that is referring to your array list variable in the main method.

正如许多人已经建议的那样,使用 Arrays.asList。但是在代码工作之前,您仍然需要修复格式,因为您在 main 方法之外有代码,这些代码引用了 main 方法中的数组列表变量。

    public static void main(String[] args){

    String s[]={"Sam","Tom","Jerry"};
    ArrayList al=new ArrayList();
    al.add(Arrays.asList(s));

    Iterator it=al.iterator();
    while(it.hasNext())
    {

        String element=String.valueOf(it.next());
        System.out.print("Element"+element);
    }
}

回答by shane

I did the following to store arrays in a ArrayList. The declaration is:

我执行以下操作将数组存储在 ArrayList 中。声明是:

ArrayList<String[]> training = new ArrayList<String[]>();


To input the words and add it:


要输入单词并添加它:

String input = sc.nextLine();
s1 = input.split(" ");
training.add(s1);


The split method splits the string with spaces and stores each word in the respective index in array s1 which was already declared with the size required for the program."sc" is the scanner object already declared.The individual arrays can be accessed by using:


split 方法用空格分割字符串,并将每个单词存储在数组 s1 中的相应索引中,该索引已经声明为程序所需的大小。“sc”是已经声明的扫描仪对象。可以使用以下方法访问各个数组:

String s4[] = training.get(index_of_array_you_want);

回答by Yogesh Rathi

Element[] array = {new Element(1), new Element(2), new Element(3)};

元素[]数组={新元素(1),新元素(2),新元素(3)};

ArrayList arr=new ArrayList(Arrays.asList(array))

ArrayList arr=new ArrayList(Arrays.asList(array))