英文:
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("enter all names separated by commas");
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;
}
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<String> 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("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");
}
答案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<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
答案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("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);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论