TreeSet<int>为什么不对
时间: 2023-08-11 19:04:41 浏览: 40
在Java中,`TreeSet`是一个基于红黑树实现的集合类,它是用于存储元素的集合,而不是用于存储基本数据类型的。因此,你不能直接声明一个`TreeSet<int>`。
如果你想存储int类型的元素,可以使用`TreeSet<Integer>`。例如:
```java
TreeSet<Integer> set = new TreeSet<>();
set.add(1);
set.add(2);
set.add(3);
System.out.println(set); // 输出 [1, 2, 3]
```
这样就可以创建一个存储int类型元素的`TreeSet`集合了。
相关问题
如何把public TreeSet<Student> treeSet = new TreeSet<>();里入对象
要往 `TreeSet` 中添加对象,你需要先创建一个 `Student` 对象,然后调用 `treeSet` 的 `add` 方法将其加入集合中。例如:
```java
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;
}
@Override
public int compareTo(Student other) {
// 根据年龄升序排序
return Integer.compare(this.age, other.age);
}
}
// 创建一个 TreeSet 对象
TreeSet<Student> treeSet = new TreeSet<>();
// 往集合中添加 Student 对象
Student s1 = new Student("Tom", 20);
treeSet.add(s1);
Student s2 = new Student("Jerry", 18);
treeSet.add(s2);
```
注意,要让 `TreeSet` 能够正确排序,你需要在 `Student` 类中实现 `Comparable` 接口,并重写 `compareTo` 方法,以指定按照哪个属性进行排序。在上面的例子中,我们按照学生的年龄进行升序排序。
如何在public TreeSet<Student> treeSet = new TreeSet<>();里加入对象
要向 `TreeSet` 中添加对象,你需要先创建一个 `Student` 对象,然后通过 `treeSet.add(student)` 方法将其添加到 `TreeSet` 中。请注意,`Student` 类必须实现 `Comparable<Student>` 接口并实现 `compareTo(Student)` 方法,以便 `TreeSet` 可以对其进行排序。
例如,假设你有一个 `Student` 类,如下所示:
```java
public class Student implements Comparable<Student> {
private int id;
private String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
@Override
public int compareTo(Student o) {
return Integer.compare(this.id, o.id);
}
}
```
现在,你可以创建一个 `Student` 对象并将其添加到 `TreeSet` 中,如下所示:
```java
TreeSet<Student> treeSet = new TreeSet<>();
Student student1 = new Student(1, "Alice");
Student student2 = new Student(2, "Bob");
treeSet.add(student1);
treeSet.add(student2);
```
这将创建一个 `TreeSet` 对象并向其中添加两个 `Student` 对象。在这个例子中,`TreeSet` 会根据 `id` 属性对 `Student` 对象进行排序。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)