用ArrayList替换(Replacing with ArrayList)

编程入门 行业动态 更新时间:2024-10-27 21:23:44
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 } } }

顺便说一句,如果对你没有任何意义,我很抱歉。 我很难解释我的意思。 但我尝试用细节解释我需要的一切。

How do I remove duplicated numbers and ask the user to add new numbers to replace the duplicated ones? Let's say the user asks for 5 numbers: 6, 7, 8, 7, 9 using the code I made, the output is Number 7 was duplicated in position 3

Now what I need help with, is asking the user to add a new number to replace that 7 so there wouldn't be anymore duplicates.

So I want the entire output to be Your numbers without duplicates: and it would print the numbers that were input without any duplications.

The order in which they are entered are important to me because I want to keep testing out ArrayList and try not to confuse myself. This is my code:

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 } } }

By the way, I'm sorry if it doesn't make any sense to you. I have a very hard time explaining what I mean. But I tried explaining everything I needed with detail.

最满意答案

我猜你需要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]); } }

I guess you need ArrayList.set() method.

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]); } }

更多推荐

本文发布于:2023-07-08 00:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1070173.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ArrayList   Replacing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!