如何访问 ArrayList 中的 ArrayList? Java
How to access an ArrayList in an ArrayList? Java
首先让我告诉你我想做什么。
该程序应要求输入密码,然后为密码的每个字符创建数组列表。
然后将这些列表添加到整个数组列表中。(我的列表)
问题是我如何用字符串值填充 ArrayLists?
当所有的数组列表都被填充后,我希望能够按字母顺序对它们进行排序。
代码吹.
// Creating ArrayLists
ArrayList<ArrayList<String>> mylist = new ArrayList<ArrayList<String>>();
List<String> mycode = new ArrayList<String>();
char[] test;
String mypassword;
System.out.println("Pleaseinput your passphrase");
// Getting user input (Password)
Scanner in = new Scanner(System.in);
mypassword = in.nextLine();
// Storing password in character array
test= mypassword.toCharArray();
// Beginning for loop to create array lists for each character
// in the password
for (int i = 0; i < mypassword.length(); i++) {
mycode = new ArrayList<String>();
// Here I'm trying to give the created Arraylist (mycode)
// a name or value of the character the for loop is at so I can order it alphabetically later.
mycode.equals(test[i]);
// Adds the array list.
mylist.add((ArrayList<String>) mycode);
}
提前感谢您对我的问题提出的任何建议。
假设根据注释,您只想将 String
转换为 List<String>
,其中每个字符都是 String
,但希望将每个字符作为第一个元素当字符为 q
...
时,列表和 "hello" 作为第二个元素
public static void main(String[] args) {
// Creating ArrayLists
List<List<String>> mylist = new ArrayList<>();
String mypassword;
System.out.println("Pleaseinput your passphrase:");
// Getting user input (Password)
Scanner in = new Scanner(System.in);
mypassword = in.nextLine();
// Beginning for loop to create array lists for each character
// in the password
for (int i = 0; i < mypassword.length(); i++) {
List<String> firstCharAndMore = new ArrayList<>();
char c = mypassword.charAt(i);
firstCharAndMore.add(String.valueOf(c));
if ('q' == c) {
firstCharAndMore.add("hello");
}
mylist.add(firstCharAndMore);
}
System.out.println(mylist.toString());
}
输入qwertyqwerty
输出:[[q, hello], [w], [e], [r], [t], [y], [q, hello], [w], [e], [r], [t], [y]]
首先让我告诉你我想做什么。 该程序应要求输入密码,然后为密码的每个字符创建数组列表。 然后将这些列表添加到整个数组列表中。(我的列表) 问题是我如何用字符串值填充 ArrayLists? 当所有的数组列表都被填充后,我希望能够按字母顺序对它们进行排序。 代码吹.
// Creating ArrayLists
ArrayList<ArrayList<String>> mylist = new ArrayList<ArrayList<String>>();
List<String> mycode = new ArrayList<String>();
char[] test;
String mypassword;
System.out.println("Pleaseinput your passphrase");
// Getting user input (Password)
Scanner in = new Scanner(System.in);
mypassword = in.nextLine();
// Storing password in character array
test= mypassword.toCharArray();
// Beginning for loop to create array lists for each character
// in the password
for (int i = 0; i < mypassword.length(); i++) {
mycode = new ArrayList<String>();
// Here I'm trying to give the created Arraylist (mycode)
// a name or value of the character the for loop is at so I can order it alphabetically later.
mycode.equals(test[i]);
// Adds the array list.
mylist.add((ArrayList<String>) mycode);
}
提前感谢您对我的问题提出的任何建议。
假设根据注释,您只想将 String
转换为 List<String>
,其中每个字符都是 String
,但希望将每个字符作为第一个元素当字符为 q
...
public static void main(String[] args) {
// Creating ArrayLists
List<List<String>> mylist = new ArrayList<>();
String mypassword;
System.out.println("Pleaseinput your passphrase:");
// Getting user input (Password)
Scanner in = new Scanner(System.in);
mypassword = in.nextLine();
// Beginning for loop to create array lists for each character
// in the password
for (int i = 0; i < mypassword.length(); i++) {
List<String> firstCharAndMore = new ArrayList<>();
char c = mypassword.charAt(i);
firstCharAndMore.add(String.valueOf(c));
if ('q' == c) {
firstCharAndMore.add("hello");
}
mylist.add(firstCharAndMore);
}
System.out.println(mylist.toString());
}
输入qwertyqwerty
输出:[[q, hello], [w], [e], [r], [t], [y], [q, hello], [w], [e], [r], [t], [y]]