Java:如何对从方法返回的列表进行写入/读取?

huangapple go评论76阅读模式
英文:

Java: how do you write/read from a list which was returned from a method?

问题

我有一个数组和一个用来从数组中的值创建列表的方法。

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);    
    System.out.println("输入所有以逗号分隔的名称");
    String allNames = scanner.nextLine();
    String[] letters = {"a", "b", "c", "d"};
    convertToList(letters);       
}

public static ArrayList<String> convertToList(String[] array) { 
    ArrayList<String> outputList = new ArrayList<>();
    for (int i = 0; i<array.length; i++) {
        outputList.add(array[i]);
    }
    return outputList;
}

现在,如果我想要在outputList中打印一个字符串或在主方法中添加一个新字符串,我该如何做?

英文:

I have an array and a method to create a list out of the values in the array.

    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);    
    System.out.println(&quot;enter all names separated by commas&quot;);
    String allNames = scanner.nextLine();
    String[] letters = {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;};;
    convertToList(letters);       
}


public static ArrayList&lt;String&gt; convertToList(String [] array) { 
    ArrayList&lt;String&gt; outputList = new ArrayList&lt;&gt;();
    for (int i = 0; i&lt;array.length; i++) {
        outputList.add(array[i]);
    }
    return outputList;
}

Now, if I wanted to print a string from outputList or add a new string to it in the main method, how do I do it?

答案1

得分: 1

ArrayList<String> myList = convertToList(letters);

然后你可以通过以下方式读取

myList.get(index)
英文:
ArrayList&lt;String&gt; myList = convertToList(letters);

And then you can read by using :

myList.get(index)

答案2

得分: 1

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("enter all names separated by commas");
    String allNames = scanner.nextLine();
    String[] letters = {"a", "b", "c", "d"};

    ArrayList<String> myList = convertToList(letters);
    // prints "a"
    System.out.println(myList.get(0));
    // add
    myList.add("e");
}
英文:
public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);    
    System.out.println(&quot;enter all names separated by commas&quot;);
    String allNames = scanner.nextLine();
    String[] letters = {&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;};

    ArrayList&lt;String&gt; myList = convertToList(letters);
    // prints &quot;a&quot;
    System.out.println(myList.get(0));
    // add
    myList.add(&quot;e&quot;);
}

答案3

得分: 1

你可以使用ArrayList作为给定方法的返回类型。

ArrayList<String> list = convertToList(letters);
list.get(0); //a
list.add("e"); //add another string
System.out.println(list); //output:- a b c d e f
英文:

You can use ArrayList as return type is ArrayList for given method

    ArrayList&lt;String&gt; list = convertToList(letters); 
    list.get(0); //a
    list.add(&quot;e&quot;); //add another string
    System.out.println(list); //output:- a b c d e f 

答案4

得分: 1

public class Foo {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("enter all names separated by commas");
        List<String> names = convertToList(scan.nextLine().split(","));

        System.out.println();
        print(names);

        names.add("John Doe");
        System.out.println();
        print(names);
    }

    public static List<String> convertToList(String[] names) {
        return Arrays.asList(names);
    }

    private static void print(List<String> names) {
        for (String name : names)
            System.out.println(name);
    }

}
英文:
public class Foo {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println(&quot;enter all names separated by commas&quot;);
        List&lt;String&gt; names = convertToList(scan.nextLine().split(&quot;,&quot;));

        System.out.println();
        print(names);

        names.add(&quot;John Doe&quot;);
        System.out.println();
        print(names);
    }

    public static List&lt;String&gt; convertToList(String[] names) {
        return Arrays.asList(names);
    }

    private static void print(List&lt;String&gt; names) {
        for (String name : names)
            System.out.println(name);
    }

}

huangapple
  • 本文由 发表于 2020年10月21日 18:08:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64461267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定