用Java的方式create an ArrayList of strings representing a list of your hobbies,
时间: 2024-03-21 12:39:23 浏览: 15
好的,以下是用 Java 创建一个包含自己爱好列表的字符串类型的 ArrayList 的代码:
```
import java.util.ArrayList;
public class HobbiesList {
public static void main(String[] args) {
// 创建一个新的字符串类型的 ArrayList
ArrayList<String> hobbies = new ArrayList<String>();
// 添加自己的爱好到列表中
hobbies.add("阅读");
hobbies.add("旅行");
hobbies.add("健身");
hobbies.add("烹饪");
hobbies.add("学习新技能");
// 打印输出自己的爱好列表
System.out.println("我的爱好包括:");
for (String hobby : hobbies) {
System.out.println(hobby);
}
}
}
```
你可以根据自己的实际情况修改此代码,并将自己的爱好列表添加到 `hobbies.add()` 方法中。
相关问题
用java, 1.In this assignment, you are asked to read from a file “student_hobbies_JU.txt” which contains your classmates’ names and their hobbies. You will then need to find out, for each of your hobbies, how many people in your class share the same hobby with you.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.
Sure, I can help you with that. Here is the solution to your problem using Java:
Task 1: Create an ArrayList of strings representing a list of your hobbies.
```java
ArrayList<String> hobbies = new ArrayList<String>();
hobbies.add("Reading");
hobbies.add("Swimming");
hobbies.add("Cooking");
hobbies.add("Playing basketball");
```
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.
```java
import java.util.*;
import java.io.*;
public class Student {
String name;
ArrayList<String> hobbies = new ArrayList<String>();
public Student(String name, ArrayList<String> hobbies) {
this.name = name;
this.hobbies = hobbies;
}
public String getName() {
return name;
}
public ArrayList<String> getHobbies() {
return hobbies;
}
}
public class Main {
public static void main(String[] args) throws IOException {
File file = new File("student_hobbies.txt");
Scanner sc = new Scanner(file);
ArrayList<Student> students = new ArrayList<Student>();
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] parts = line.split(" ");
String name = parts[0];
ArrayList<String> hobbies = new ArrayList<String>();
for (int i = 1; i < parts.length; i++) {
hobbies.add(parts[i]);
}
Student student = new Student(name, hobbies);
students.add(student);
}
sc.close();
}
}
```
Task 3: Sort the list of students by ascending order of names.
```java
Collections.sort(students, new Comparator<Student>() {
public int compare(Student s1, Student s2) {
return s1.getName().compareTo(s2.getName());
}
});
```
Task 4: For each of your hobby, find out how many students in the student ArrayList share that hobby with you.
```java
for (String hobby : hobbies) {
int count = 0;
for (Student student : students) {
if (student.getHobbies().contains(hobby)) {
count++;
}
}
System.out.println("Number of students who share the hobby " + hobby + " with you: " + count);
}
```
I hope this helps you! 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()方法中修改相应的文件路径。