import java.util.*; public class Task { public static void main(String[] args) { List<String> names = new ArrayList<>(Arrays.asList(args)); Collections.addAll(names, "John", "Kate", "Tom", "Bob", "Alice"); // 排序---------------------------------------------- /* TODO 对names集合进行排序 */ Collections.sort(names); System.out.println(names); // 打乱----------------------------------------------- Random random = new Random(names.size()); /* TODO 使用random打乱names集合 */ System.out.println(names); } }
时间: 2023-05-19 13:05:44 浏览: 120
对names集合进行排序的代码如下:
Collections.sort(names);
使用random打乱names集合的代码如下:
Collections.shuffle(names, random);
相关问题
用JavaSort the Student list by ascending order of student namesThe following is an example illustrating formatting output. You will need it for task 2 and 3. public class Main { public static void main(String[] args) { String string1 = "Hello"; String string2 = "World"; String string3 = "Java"; System.out.println(String.format("%-10s %-10s %-10s", string1, string2, string3)); } }
Sure, here's an example Java code that sorts a list of students by ascending order of their names using the `Collections.sort()` method and formats the output using `String.format()`:
```java
import java.util.ArrayList;
import java.util.Collections;
public class Student implements Comparable<Student> {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public int compareTo(Student other) {
return this.getName().compareTo(other.getName());
}
public String toString() {
return String.format("%-10s %-10d", this.getName(), this.getAge());
}
}
public class Main {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
students.add(new Student("John", 20));
students.add(new Student("Alice", 19));
students.add(new Student("Bob", 21));
students.add(new Student("Charlie", 18));
System.out.println("Before sorting:");
for (Student student : students) {
System.out.println(student);
}
Collections.sort(students);
System.out.println("After sorting:");
for (Student student : students) {
System.out.println(student);
}
}
}
```
In this example, the `Student` class implements the `Comparable` interface and overrides the `compareTo()` method to compare students based on their names. The `toString()` method formats the output using `String.format()` with the `%10s` and `%10d` format specifiers for the name and age fields, respectively. In the `Main` class, we create a list of students, print the list before sorting, sort the list using `Collections.sort()`, and print the list after sorting. The output will be:
```
Before sorting:
John 20
Alice 19
Bob 21
Charlie 18
After sorting:
Alice 19
Bob 21
Charlie 18
John 20
```
I hope this helps! Let me know if you have any other questions.
Task 1: create an ArrayList of strings representing a list of your hobbies, • Task 2: read from a file “student_hobbies.txt” and store the information in the file into an Arraylist of Student-type object. You will need to create a Student class for this. • Task 3: Sort the list of students by ascending order of names. • Task 4: For each of your hobby, find out how many students in the student Arraylist share that hobby with you. An example running result is given in Figure 2. This AppDriver.java file contains compile errors because of the missing method definitions, for example, createHobbies(), readFile(), and findSharedHobbies(). Do not change the main(). Just add definitions of the above methods to your program. Specifically, create createHobbies() for Task 1, readFile() for Task 2, and findSharedHobbies() for Task 4. For the sorting inTask 3 to work appropriately, you will need to have the Student class implement the Comparable interface.
好的,让我来帮您实现这个程序。以下是完整的代码,其中包含了Task 1到Task 4的实现:
```java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
public class AppDriver {
private static ArrayList<String> hobbies = new ArrayList<String>();
private static ArrayList<Student> students = new ArrayList<Student>();
public static void main(String[] args) {
createHobbies();
readFile();
sortStudents();
findSharedHobbies();
}
// Task 1: create an ArrayList of strings representing a list of your hobbies
public static void createHobbies() {
hobbies.add("Reading");
hobbies.add("Swimming");
hobbies.add("Playing video games");
hobbies.add("Watching movies");
}
// Task 2: read from a file “student_hobbies.txt” and store the information in the file into an Arraylist of Student-type object.
public static void readFile() {
try {
File file = new File("student_hobbies.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] parts = line.split(",");
String name = parts[0].trim();
String hobby = parts[1].trim();
students.add(new Student(name, hobby));
}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("Cannot find file: student_hobbies.txt");
}
}
// Task 3: Sort the list of students by ascending order of names.
public static void sortStudents() {
Collections.sort(students);
}
// Task 4: For each of your hobby, find out how many students in the student Arraylist share that hobby with you.
public static void findSharedHobbies() {
for (String hobby : hobbies) {
int count = 0;
for (Student s : students) {
if (s.getHobby().equals(hobby)) {
count++;
}
}
System.out.println("There are " + count + " students who share the hobby of " + hobby);
}
}
}
class Student implements Comparable<Student> {
private String name;
private String hobby;
public Student(String name, String hobby) {
this.name = name;
this.hobby = hobby;
}
public String getName() {
return this.name;
}
public String getHobby() {
return this.hobby;
}
@Override
public int compareTo(Student s) {
return this.name.compareTo(s.getName());
}
}
```
在上述代码中,我们首先定义了一个静态的ArrayList变量hobbies来存储我们自己的爱好。然后,我们实现了Task 1,即在createHobbies()方法中向hobbies变量中添加了一些爱好。接着,我们实现了Task 2,即在readFile()方法中读取了文件“student_hobbies.txt”中的内容,并将每个学生的姓名和爱好存储到了一个Student对象中,并将这些对象存储到了静态的ArrayList变量students中。然后,我们实现了Task 3,即在sortStudents()方法中使用Collections.sort()函数对students变量进行升序排序。最后,我们实现了Task 4,即在findSharedHobbies()方法中遍历hobbies变量中的每个爱好,并统计有多少个学生的爱好与之相同,并输出结果。
注意,在实现Task 3时,我们需要让Student类实现Comparable接口,并覆盖其compareTo()方法,以便可以在Collections.sort()函数中使用。在compareTo()方法中,我们只需要比较两个Student对象的姓名即可。
请确保在运行程序之前将“student_hobbies.txt”文件放置在正确的路径下,并且文件内容格式为“姓名,爱好”的形式,例如:
```
Tom,Swimming
Alice,Reading
Bob,Running
David,Singing
```
如果您需要对文件路径进行更改,请在readFile()方法中修改相应的文件路径。
阅读全文