简体   繁体   English

在对象数组中查找重复项

[英]Finding duplicates in an array of objects

The purpose of this project is to make a pokedex that adds and holds all the pokemon passed in by user input. 该项目的目的是制作一个可添加和保存用户输入传递的所有宠物小精灵的pokedex。 When the user inputs a pokemon that is already stored in the pokedex the word "duplicate" is supposed to be printed to the console. 当用户输入已经存储在口袋妖怪中的口袋妖怪时,单词“重复”应该被打印到控制台上。 The word duplicate is printed even though there are no actual duplicates within the object array. 即使对象数组中没有实际的重复项,也会打印重复项。 Here is my output from the console : 这是控制台的输出:

Welcome to your new PokeDex! 欢迎使用您的新PokeDex! How many Pokemon are in your region?: 3 您所在地区有多少只神奇宝贝?:3

Your new Pokedex can hold 3 Pokemon. 您的新Pokedex可以容纳3个Pokemon。 Let's start using it! 让我们开始使用它!

  1. List Pokemon 列出口袋妖怪
  2. Add Pokemon 添加口袋妖怪
  3. Check a Pokemon's Stats 查看口袋妖怪的状态
  4. Sort Pokemon 排序口袋妖怪
  5. Exit 出口

What would you like to do? 你想干什么? 2 2

Please enter the Pokemon's Species: red Duplicate 请输入神奇宝贝的种类:红色

Now here is all the code used that could possibly be making this error 现在这是所有可能导致此错误的代码

import java.util.Scanner;

public class Project4 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Welcome to your new PokeDex!");
        System.out.print("How many Pokemon are in your region?: ");
        int size = input.nextInt();
        Pokedex pokedex = new Pokedex(size);
        System.out.println("\nYour new Pokedex can hold " + size + " Pokemon. Let's start using it!");
        int choice = 0;
        boolean done = false;

        while (!done) {
            System.out.println("\n1. List Pokemon\n2. Add Pokemon\n3. Check a Pokemon's Stats" + "\n4. Sort Pokemon\n5. Exit");
            System.out.print("\nWhat would you like to do? ");
            choice = input.nextInt();

            switch (choice) {
                case 1:
                    String[] pokemonList = pokedex.listPokemon();
                    if (pokemonList == null)
                        System.out.println("Empty");
                    else
                        for (int i = 0; i < pokemonList.length; i++) {
                            System.out.println((i + 1) + ". " + pokemonList[i]);
                        }
                    break;
                case 2:
                    System.out.print("\nPlease enter the Pokemon's Species: ");
                    String species = input.next();
                    pokedex.addPokemon(species);
                    break;
            }
        }
    }
}

In the following class I have the actual method that adds the pokemon and the constructor for Pokedex 在下面的类中,我有为Pokedex添加神奇宝贝和构造函数的实际方法

public class Pokedex {
    Pokemon[] pokedex;
    String pokeArray[];

    public Pokedex(int size) {
        pokedex = new Pokemon[size];
        pokeArray = new String[size];
    }

    public boolean addPokemon(String species) {
        Pokemon stuff = new Pokemon(species);

        for (int i = 0; i < pokedex.length; i++) {
            if (pokedex[i] == null) {
                pokedex[i] = stuff;
            }

            else if (i < pokedex.length && pokedex[i] != null) {
                System.out.println("Max");
            }

            if (pokedex[i].getSpecies().equalsIgnoreCase(species)) {
                System.out.print("Duplicate");
                break;
            }
        }

        return false;
    }
} 

Sorry for the mass amounts of code I just need help tracing where this unexpected result is coming from. 对不起,大量代码我只需要帮助跟踪此意外结果的来源。

The reason it's doing that is because of this bit of code here: 这样做的原因是由于以下这段代码:

public boolean addPokemon(String species)
{
    Pokemon stuff = new Pokemon(species);
    for (int i = 0; i < pokedex.length; i++)
    {
        if (pokedex[i] == null)
            pokedex[i] = stuff;
        else if (i < pokedex.length && pokedex[i] !=null)
            System.out.println("Max");
        if(pokedex[i].getSpecies().equalsIgnoreCase(species))
        {
            System.out.print("Duplicate");
            break;
        }
    }
   return false;
}

The problem is just a little bit of syntax missing. 问题只是缺少一点语法。 In your for loop, you check to see if 在您的for循环中,您检查是否

A) there are any empty spots in the array A)阵列中有任何空白点

B) if every element in the array up to the user inputted size is full B)如果数组中直到用户输入大小的每个元素都已满

and C) if any element in the array matches the one we're trying to add. C)如果数组中的任何元素与我们尝试添加的元素匹配。

The problem you're encountering is because your C is an if instead of an else if . 您遇到的问题是因为您的C是一个if而不是else if Because A sees the index is null, it assigns the new Pokemon to the Pokedex. 因为A看到索引为空,所以它将新的Pokemon分配给Pokedex。 Then because C is an if instead of an else if, it runs after you assign the new Pokemon and sees the Pokemon we just added and says it's a duplicate. 然后,由于C是if而不是else if,因此它将在您分配新的Pokemon后运行,并看到我们刚刚添加的Pokemon并说它是重复的。 Changing it to an else if would fix this. 如果将其更改为其他,则可以解决此问题。

Also, since there was no break; 另外,由于没有break; in A, it would assign every element of the array to the first one entered, causing any further additions to call Max. 在A中,它将把数组的每个元素分配给输入的第一个元素,从而导致进一步的增加来调用Max。 I edited the code and this is what I had that worked for me: 我编辑了代码,这就是对我有用的东西:

public boolean addPokemon(String species)
{
    Pokemon stuff = new Pokemon(species);
    for (int i = 0; i < pokedex.length; i++)
    {
        if(pokedex[i] !=null && pokedex[i].getSpecies().equalsIgnoreCase(species))
        {
            System.out.println("Duplicate");
            break;
        }
        else if (pokedex[i] == null)
        {
            pokedex[i] = stuff;
            break;
        }
        else if(i + 1 == pokedex.length)
        {
            System.out.println("Max");
            break;
        }
    }
   return false;
}

Also, out of curiosity, why is the addPokemon() function a boolean? 另外,出于好奇,为什么addPokemon()函数为布尔值? You return a value (albeit arbitrarily) and then never do anything with that value. 您返回一个值(尽管是任意的),然后永远不要对该值执行任何操作。 You could just make it a void, have it return nothing, and it would work just as fine. 您可以将其设为空,使其不返回任何内容,并且效果会一样好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM