替换为 ArrayList
Replacing with ArrayList
如何删除重复的号码并要求用户添加新号码来替换重复的号码?假设用户要求 5
个数字:6, 7, 8, 7, 9
使用我编写的代码,输出是 Number 7 was duplicated in position 3
现在我需要帮助的是,要求用户添加一个新号码来替换那个 7,这样就不会再有重复的号码了。
所以我希望整个输出为 Your numbers without duplicates:
并且它会打印输入的数字而不会出现任何重复。
它们的输入顺序对我来说很重要,因为我想继续测试 ArrayList 并尽量不要混淆自己。
这是我的代码:
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JOptionPane;
public class Test {
public static void main(String[] args) {
ArrayList<Integer> al = new ArrayList<Integer>();
int opt = Integer.parseInt(JOptionPane.showInputDialog("How many numbers?");
for (int i=0 ; i < opc ; i++) {
al.add(Integer.parseInt(JOptionPane.showInputDialog("Which numbers?")));
}
Set<Integer> s = new HashSet<>();
for (Integer d : al){
if (s.add(d) == false)
JOptionPane.showMessageDialog(null,"Number " + d + " was duplicated in position " + al.lastIndexOf(d));
JOptionPane.showMessageDialog(null,"Replace new number"); //This is where I would like to replace the numbers if possible
}
JOptionPane.showMessageDialog("Your numbers without duplicates: "); //This is where it would print
}
}
}
顺便说一句,如果这对您没有任何意义,我深表歉意。我很难解释我的意思。但我试着详细解释我需要的一切。
与其执行 s.add(d)
并检查 false
,然后尝试替换重复的数字,不如使用 s.contains(d)
检查重复项。这不会将该号码添加到列表中,当用户提供另一个非重复号码时,您可以将其添加到列表中。
我猜你需要 ArrayList.set() 方法。
for (Integer d : al){
while (s.add(d) == false){ //replace if with while to keep looking for duplicate
JOptionPane.showMessageDialog(null,"Number " + d + " was duplicated in position " + al.lastIndexOf(d));
JOptionPane.showMessageDialog(null,"Replace new number");
// al.set(d,[set the new value here]);
}
}
如何删除重复的号码并要求用户添加新号码来替换重复的号码?假设用户要求 5
个数字:6, 7, 8, 7, 9
使用我编写的代码,输出是 Number 7 was duplicated in position 3
现在我需要帮助的是,要求用户添加一个新号码来替换那个 7,这样就不会再有重复的号码了。
所以我希望整个输出为 Your numbers without duplicates:
并且它会打印输入的数字而不会出现任何重复。
它们的输入顺序对我来说很重要,因为我想继续测试 ArrayList 并尽量不要混淆自己。 这是我的代码:
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JOptionPane;
public class Test {
public static void main(String[] args) {
ArrayList<Integer> al = new ArrayList<Integer>();
int opt = Integer.parseInt(JOptionPane.showInputDialog("How many numbers?");
for (int i=0 ; i < opc ; i++) {
al.add(Integer.parseInt(JOptionPane.showInputDialog("Which numbers?")));
}
Set<Integer> s = new HashSet<>();
for (Integer d : al){
if (s.add(d) == false)
JOptionPane.showMessageDialog(null,"Number " + d + " was duplicated in position " + al.lastIndexOf(d));
JOptionPane.showMessageDialog(null,"Replace new number"); //This is where I would like to replace the numbers if possible
}
JOptionPane.showMessageDialog("Your numbers without duplicates: "); //This is where it would print
}
}
}
顺便说一句,如果这对您没有任何意义,我深表歉意。我很难解释我的意思。但我试着详细解释我需要的一切。
与其执行 s.add(d)
并检查 false
,然后尝试替换重复的数字,不如使用 s.contains(d)
检查重复项。这不会将该号码添加到列表中,当用户提供另一个非重复号码时,您可以将其添加到列表中。
我猜你需要 ArrayList.set() 方法。
for (Integer d : al){
while (s.add(d) == false){ //replace if with while to keep looking for duplicate
JOptionPane.showMessageDialog(null,"Number " + d + " was duplicated in position " + al.lastIndexOf(d));
JOptionPane.showMessageDialog(null,"Replace new number");
// al.set(d,[set the new value here]);
}
}