去重复数字代码完善:import java.util.ArrayList; public class Main { public static void main(String[] args) { int[] arr = {1, 1, 2, 3}; ArrayList<Integer> list = new ArrayList<Integer>(); for (int i = 0; i < arr.length; i++) {
时间: 2024-03-17 22:47:53 浏览: 200
可以使用一个set来存储已经出现过的数字,每次遍历到一个数字时,先判断set中是否已经包含该数字,若没有则将其加入set和list中,否则跳过该数字。代码如下:
```java
import java.util.ArrayList;
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
int[] arr = {1, 1, 2, 3};
ArrayList<Integer> list = new ArrayList<Integer>();
HashSet<Integer> set = new HashSet<Integer>();
for (int i = 0; i < arr.length; i++) {
if (!set.contains(arr[i])) {
set.add(arr[i]);
list.add(arr[i]);
}
}
System.out.println(list); // [1, 2, 3]
}
}
```
相关问题
对此代码进行完善:import java.util.ArrayList; public class Main{ public static void main(String[] args){ int[] arr={1,1,2,3,}; ArrayList<Integer> list=new ArrayList<Integer>(); for(int i=0;i<list.size();i++); System.out.println(list.get(i)); } }
这段代码存在一些问题,需要进行完善:
1. 数组 `arr` 中最后一个元素后面多了一个逗号,需要去掉。
2. 在 `for` 循环中,循环条件应该是 `i < arr.length` 而不是 `i < list.size()`,因为此时 `list` 是空的,没有任何元素。
3. 在 `for` 循环中,应该将数组 `arr` 中的元素添加到 `list` 中,可以使用 `list.add(arr[i])` 方法实现。
4. 在 `for` 循环结束后,应该使用 `list.size()` 方法获取 `list` 中元素的个数,而不是 `list.get(i)`。
修改后的代码如下:
```java
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
int[] arr = {1, 1, 2, 3};
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < arr.length; i++) {
list.add(arr[i]);
}
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
```
修改后的代码可以正确地将数组中的元素添加到 `list` 中,并输出 `list` 中的元素。
声明一个 Student 类,属性包括姓名、学号、总成绩;生成 10 个 Student 类对象,并放在一个一维数组中,编写方法按总成绩进行排序,将排序后的对象 分别保持在Vector 、ArrayList、 HashTable类型的对象中,并遍历显示其中 元素的信息。 程序设计参考框架如下: import java,util.Vector; import java.util.ArrayList; import java.util.Hashtable; import java.util.Enumeration; import java.util.Iterator; public class Ex71 { public static void main (String[] args){
public class Student implements Comparable<Student>{
private String name;
private int id;
private int totalScore;
public Student(String name, int id, int totalScore){
this.name = name;
this.id = id;
this.totalScore = totalScore;
}
public String getName(){
return name;
}
public int getId(){
return id;
}
public int getTotalScore(){
return totalScore;
}
public void setName(String name){
this.name = name;
}
public void setId(int id){
this.id = id;
}
public void setTotalScore(int totalScore){
this.totalScore = totalScore;
}
public int compareTo(Student s){
return this.totalScore - s.totalScore;
}
}
import java.util.Vector;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.Iterator;
public class Ex71 {
public static void main (String[] args){
Student[] students = new Student[10];
students[0] = new Student("Tom", 1001, 80);
students[1] = new Student("Jerry", 1002, 90);
students[2] = new Student("Alice", 1003, 85);
students[3] = new Student("Bob", 1004, 70);
students[4] = new Student("Peter", 1005, 95);
students[5] = new Student("Mary", 1006, 65);
students[6] = new Student("John", 1007, 75);
students[7] = new Student("Lucy", 1008, 88);
students[8] = new Student("David", 1009, 92);
students[9] = new Student("Lily", 1010, 78);
// sort by total score
Arrays.sort(students);
// put sorted objects into Vector
Vector<Student> vector = new Vector<Student>();
for(Student s : students){
vector.add(s);
}
// put sorted objects into ArrayList
ArrayList<Student> arrayList = new ArrayList<Student>();
for(Student s : students){
arrayList.add(s);
}
// put sorted objects into Hashtable
Hashtable<Integer, Student> hashtable = new Hashtable<Integer, Student>();
int i = 1;
for(Student s : students){
hashtable.put(i++, s);
}
// display elements in Vector
System.out.println("Elements in Vector:");
Enumeration<Student> enumeration = vector.elements();
while(enumeration.hasMoreElements()){
Student s = enumeration.nextElement();
System.out.println("Name: " + s.getName() + ", ID: " + s.getId() + ", Total Score: " + s.getTotalScore());
}
// display elements in ArrayList
System.out.println("Elements in ArrayList:");
Iterator<Student> iterator = arrayList.iterator();
while(iterator.hasNext()){
Student s = iterator.next();
System.out.println("Name: " + s.getName() + ", ID: " + s.getId() + ", Total Score: " + s.getTotalScore());
}
// display elements in Hashtable
System.out.println("Elements in Hashtable:");
Enumeration<Integer> keys = hashtable.keys();
while(keys.hasMoreElements()){
Integer key = keys.nextElement();
Student s = hashtable.get(key);
System.out.println("Name: " + s.getName() + ", ID: " + s.getId() + ", Total Score: " + s.getTotalScore());
}
}
}
阅读全文