【发布时间】:2020-10-08 18:17:02
【问题描述】:
我的程序旨在分离字符串,然后将它们放入数组中。例如,如果我输入值 LONDON,我仍然会返回字符串 [LONDON] 而不是我想要的 [L, O, N, D, O, N]。
import java.io.IOException;
import java.util.*;
class Main {
public static void main(String[] args) throws IOException {
ArrayList<String> myArrayList =
new ArrayList<String>();
System.out.println("\n" +
"This is the capacity before adding any strings " +
myArrayList.size());
System.out.println("\n" +
"I will now add a string of my name seperated in the " +
"Array List");
myArrayList.add("E");
myArrayList.add("M");
myArrayList.add("M");
myArrayList.add("A");
myArrayList.add("N");
myArrayList.add("U");
myArrayList.add("E");
myArrayList.add("L");
System.out.println("\n" +
"Now let us see the size of the Array List now I have" +
"put in my name " + myArrayList.size());
System.out.println("\n" +
"Let us see what the program has put into my Array List " +
myArrayList);
System.out.println("\n" +
"Now input your own strings!");
ArrayMods arrayMods
= new ArrayMods();
while(true) {
arrayMods.inputToSeparatedArray();
System.out.println("This is your Array!" +
arrayMods.putCharString() +
"Type in your next string!");
}
}
}
class ArrayMods{
public char[] inputToSeparatedArray(){
Scanner scanInput = new Scanner(System.in);
String toBeSeparated;
toBeSeparated = scanInput.nextLine();
return toBeSeparated.toCharArray();
}
public ArrayList<String> putCharString() {
char[] receivingArray = inputToSeparatedArray();
for (int x = 0; x > receivingArray.length; x++) {
putCharString().add(String.valueOf(receivingArray[x]));
}
List<String> l = Collections.<String>singletonList(String.valueOf(receivingArray));
return new ArrayList<String>(l);
}
}
【问题讨论】:
-
您的问题不清楚。在标题中你声称你想要 ArrayList,但在问题本身你说“..然后将它们放入数组”。 ArrayList 与数组不同。使用edit 选项阐明预期结果。
标签: java arrays string arraylist