所以我有一个数组列表,其中包含一个喜欢的颜色列表。例如,数组读取为红色,蓝色,绿色。但现在我希望用户能够“排列”他们喜欢的颜色,因为当前列表不是他们的个人排名。那么我将如何将数组列表中的一个元素向下移动到另一个位置。我想让程序说“列表中的第一项是红色的,你会在哪里排名?”然后他们可以说他们希望在第三名。然后等到他们排列所有这些。问题是我是新的阵列列表,不知道如何做到这一点。我会使用Collections.sort(数组列表)吗? ?我甚至会如何请求数组中的元素来询问它们?是像(在这里使用伪代码)数组列表的第1行/元素1?根据用户输入对数组列表进行排序?
到目前为止的代码(最后一部分没有真正正常工作,但你会看到我试图去)
Scanner input = new Scanner(System.in);
ArrayList <String> favoriteColors = new ArrayList <String>();
boolean repeat = true;
while (repeat) {
System.out.println("Enter the name of the file which contains your favorite colors ");
String fileName = input.nextLine().trim();
try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {
String line;
System.out.println("Here are your favorite colors according to the file:");
while ((line = reader.readLine()) != null) {
System.out.println(line);
favoriteColors.add((line));
}
System.out.println("Add more? (y/n)");
if (input.next().startsWith("y")) {
System.out.println("Enter : ");
favoriteColors.add(input.next());
} else {
break;
}
for (int i = 0; i < favoriteColors.size(); i++) {
System.out.println(favoriteColors);
}
System.out.println("Remove a color?");
if (input.next().startsWith("y")) {
System.out.println("Which color would you like to remove");
String removeColor = input.nextLine();
int index = favoriteColor.indexOf(removeColor);
favoriteColor.remove(index);
System.out.println(favoriteColor);
}
感谢
你可以发布你有什么代码,你做了什么?至少需要用于初始化数组列表的后期代码。你使用的是Python列表,numpy数组还是数组? – imp9
@ imp9只是加了我的代码 –