【发布时间】:2017-05-04 19:50:29
【问题描述】:
所以我有一个数组列表,其中包含最喜欢的颜色列表。例如,数组读取为 - 红、蓝、绿。但现在我希望用户能够“排名”他们最喜欢的颜色,因为当前列表不是他们的个人排名。那么我如何将数组列表的一个元素向下移动到另一个位置。我想让程序说“列表上的第一项是红色的,你会把它排在哪里?”然后他们可以说他们想要它在第三个位置。然后以此类推,直到他们对所有这些进行排名。问题是我是数组列表的新手,不知道该怎么做。我会使用 Collections.sort(array list); ?我什至如何调用数组的元素来询问它们?是不是类似于(在此处使用伪代码)数组列表的第 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 刚刚添加了我的代码