在arraylist错误中查找唯一元素

时间:2017-01-15 02:57:20

标签: java arraylist

我是数组列表的新手。我有一个程序,我想找到独特的城市。我使用for循环来使用它,但它似乎没有显示我想要它。我可以知道我哪里出错吗?

run:
Enter name of city: Singapore
Enter size of city: Small
Enter postal code: 132115
___________________________________________
Enter name of city: Singapore
Enter size of city: Small
Enter postal code: 132115
___________________________________________
Enter name of city: Singapore
Enter size of city: Small
Enter postal code: 132115
___________________________________________
Enter name of city: Singapore
Enter size of city: Small
Enter postal code: 132115
___________________________________________
Enter name of city: Malaysia
Enter size of city: Medium
Enter postal code: 132222
___________________________________________
List Of Unique Cities: 
Malaysia,Singapore,Singapore,Singapore,Malaysia,Singapore,

我希望它像这样打印

Enter name of city: Singapore
Enter size of city: Small
Enter postal code: 132115
___________________________________________
Enter name of city: Singapore
Enter size of city: Small
Enter postal code: 132115
___________________________________________
Enter name of city: Singapore
Enter size of city: Small
Enter postal code: 132115
___________________________________________
Enter name of city: Singapore
Enter size of city: Small
Enter postal code: 132115
___________________________________________
Enter name of city: Malaysia
Enter size of city: Medium
Enter postal code: 132222
___________________________________________
List Of Unique Cities: 
Malaysia,Singapore,
public class TravellingApp {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        SalesPerson s1 = new SalesPerson();

        for(int j=1; j<=20; j++)
        {
            System.out.print("Enter name of city: ");
            String name = sc.nextLine();

            System.out.print("Enter size of city: ");
            String size = sc.nextLine();

            System.out.print("Enter postal code: ");
            int postalCode = sc.nextInt();
            sc.nextLine();

            System.out.println("___________________________________________");



            City c1 = new City(name, size, postalCode);
            s1.addCity(c1);

        }
        System.out.println("List Of Unique Cities: ");
        for (int i = 0; i < s1.returnListOfCities().size(); i++) 
        {
            for (int k = s1.returnListOfCities().size() - 1; k >= i; k--) 
            {
                if (s1.returnListOfCities().get(i).equals(s1.returnListOfCities().get(k)))
                {
                    s1.returnListOfCities().remove(s1.returnListOfCities().get(i));
                    break;
                }
                System.out.print(s1.returnListOfCities().get(k) + ",");

            }
        }

    }

    }

2 个答案:

答案 0 :(得分:1)

您的City类必须覆盖方法equals()以获得所需的equals()行为(默认只测试对象引用。此行为不是您想要的)。

顺便说一句,您甚至不需要进行此重复检测。只需切换到使用接口Set,它就可以保证集合中的所有元素都是唯一的。如果您不关心订单,那么您可以使用实现HashSet作为实现。因此,实现City#hashCode()然后切换到使用此行:

Set<City> s1 = new HashSet<>();

答案 1 :(得分:0)

如果您希望保持原始列表不变,并构建一个具有不同值的新列表,以下是使用java8流的方法:

<img>

正如已经说过和解释的那样,您的List<City> allCities = s1.returnListOfCities(); List<City> distinctCities = allCities.stream().distinct().collect(Collectors.toList()); 必须覆盖City

使用流,你也可以像这样打印你的列表:

equals()

或者,如果你不能用System.out.println(allCities.stream().distinct().map(City::getName).collect(Collectors.joining(","))); 覆盖City(假设它已经定义为在邮政编码上进行比较,但你想在名称上进行比较),你可以这样做:

equals()

在这种情况下,名称将直接比较为String。