gpt4 book ai didi

java - 如何访问 ArrayList 中的 ArrayList? java

转载 作者:行者123 更新时间:2023-11-28 21:29:14 25 4
gpt4 key购买 nike

首先让我告诉你我想做什么。该程序应要求输入密码,然后为密码的每个字符创建数组列表。然后将这些列表添加到整个数组列表中。(我的列表)问题是我如何用字符串值填充 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]]

关于java - 如何访问 ArrayList 中的 ArrayList? java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29414507/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com