删除和ArrayList的替换重复 [英] Remove and Replace Duplicates in ArrayList

查看:135
本文介绍了删除和ArrayList的替换重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何消除对ArrayList的重复号码,并更换新?

我想没有他们复制打印的数字。

这是我的code:

 进口的java.util.ArrayList;
进口java.util.HashSet中;
进口java.util.Set中;
进口javax.swing.JOptionPane中;公共类的测试{
    公共静态无效的主要(字串[] args){
        ArrayList的<整数GT;人=新的ArrayList<整数GT;();        INT选择=的Integer.parseInt(JOptionPane.showInputDialog(多少号?);
        的for(int i = 0; I< OPC;我++){
            al.add(的Integer.parseInt(JOptionPane.showInputDialog(哪个数字?)));
        }        SET<整数GT; S =新的HashSet<>();
        对于(整数D:人){
            如果(s.add(四)== FALSE)
                JOptionPane.showMessageDialog(NULL,数字+ D + + al.lastIndexOf(D位置被复制));
                JOptionPane.showMessageDialog(NULL,更换新号码); //这是我想如果可能的话来代替数字
            }
        JOptionPane.showMessageDialog(你的号码没有重复:); //这是它将打印
        }
    }
}


解决方案

prevent进入重复号码,当用户输入他们,而不是检查和后来替换它们。搜索结果做,在这个地方

 的for(int i = 0; I< OPC;我++){
    al.add(的Integer.parseInt(JOptionPane.showInputDialog(哪个数字?)));
}

Chceck如果一个数字已经存在,如果是,则要求另一个不重复,如果是,则添加它,并要求下一个数字:

 的for(int i = 0; I< OPC;我++){
    INT为mynumber =的Integer.parseInt(JOptionPane.showInputDialog(哪个数字?));
    而(真){
        如果(!al.contains(mynumber的))
           al.add(mynumber的);
           打破;
        }
        为mynumber =的Integer.parseInt(JOptionPane.showInputDialog(这个数字是一个重复,再输入另一号码));
    }
}

结果
========编辑================== 搜索结果
我已经纠正了这个例子。还有就是缺少 {如果在上面的(previous)之一:

 公共类MmuClass {    公共静态无效的主要(字符串... wwwx){
        清单<整数GT; LST = Arrays.asList(4,2,6,-6,9);        ArrayList的<整数GT;人=新的ArrayList<整数GT;();        的for(int i = 0;我小于5;我++){
            INT为mynumber =的Integer.parseInt(JOptionPane.showInputDialog(哪个数字?));
            而(真){
                如果(!al.contains(mynumber的)){
                    al.add(mynumber的);
                    打破;
                }
                为mynumber =的Integer.parseInt(
                        JOptionPane.showInputDialog(这个数字是一个重复,再输入另一号码));
            }
            。al.stream()的forEach(的System.out ::的println);
        }
    }
}

How do I remove duplicate numbers on ArrayList and replace them with new ones?

I want to print the numbers without them duplicating.

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,"The 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
        }
    }
}

解决方案

Prevent from entering duplicate numbers when the user enters them, instead of checking and replacing them later.

Do it in this place:

for (int i=0 ; i < opc ; i++) {
    al.add(Integer.parseInt(JOptionPane.showInputDialog("Which numbers?")));
}

Chceck if a number already exists, if yes then ask for another not duplicated, if yes then add it and ask for next number :

for (int i=0 ; i < opc ; i++) {
    int myNumber = Integer.parseInt(JOptionPane.showInputDialog("Which numbers?"));
    while( true ){
        if( ! al.contains( myNumber ))
           al.add( myNumber );
           break;
        }
        myNumber = Integer.parseInt(JOptionPane.showInputDialog("This number is a duplicate, enter another number again"));
    }
}


======== EDIT ==================

I've corrected the example. There is missing { after if in the above (previous) one:

public class MmuClass {

    public static void main(String... wwwx) {
        List<Integer> lst = Arrays.asList(4, 2, 6, -6, 9);

        ArrayList<Integer> al = new ArrayList<Integer>();

        for (int i = 0; i < 5; i++) {
            int myNumber = Integer.parseInt(JOptionPane.showInputDialog("Which numbers?"));
            while (true) {
                if (!al.contains(myNumber)) {
                    al.add(myNumber);
                    break;
                }
                myNumber = Integer.parseInt(
                        JOptionPane.showInputDialog("This number is a duplicate, enter another number again"));
            }
            al.stream().forEach(System.out::println);
        }
    }
}

这篇关于删除和ArrayList的替换重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆