Как распечатать перетасованный массив ArrayList? Это то, что у меня есть до сих пор:Как распечатать перетасованный массив ArrayList?
public class RandomListSelection {
public static void main(String[] args) {
String currentDir = System.getProperty("user.dir");
String fileName = currentDir + "\\src\\list.txt";
// Create a BufferedReader from a FileReader.
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(
fileName));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Create ArrayList to hold line values
ArrayList<String> elements = new ArrayList<String>();
// Loop over lines in the file and add them to an ArrayList
while (true) {
String line = null;
try {
line = reader.readLine();
// Add each line to the ArrayList
elements.add(line);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (line == null) {
break;
}
}
// Randomize ArrayList
Collections.shuffle(elements);
// Print out shuffled ArrayList
for (String shuffedList : elements) {
System.out.println(shuffedList);
}
// Close the BufferedReader.
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Я хочу, чтобы иметь возможность добавлять строки из моего файла в ArrayList, так что я инициализируюсь его в петле. Это плохо? Я немного изменил код. – santafebound
OK, теперь вы печатаете перетасованный ArrayList. В чем вопрос, опять? –
Хорошо, я понял это выше. Взглянуть. – santafebound