在数组列表错误中查找唯一元素



我是数组列表的新成员。我有一个计划,我想找到独特的城市。我使用 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) + ",");
            }
        }
    }
    }

City类必须重写方法equals()才能获得所需的equals()行为(默认值仅测试对象引用。 此行为不是您想要的)。

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

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

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

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(",")));

或者,如果您无法使用equals()覆盖City(假设它已经定义为在邮政编码上进行比较,但您想在名称上进行比较),您可以执行以下操作:

System.out.println(allCities.stream().map(City::getName).distinct().collect(Collectors.joining(",")));

在这种情况下,名称直接作为字符串进行比较。

最新更新