简体   繁体   中英

Passing array to array is yielding null values, what have I done wrong?

So I'm trying to write some code where I basically need to expand an array by one every time I pass through a loop. Yes I know that arrays are fixed size and that I need a new array each time. I'm also aware that if I used a list rather than an array I wouldn't be having any issues however I am being forced to use an array.

My code is so:

  public static void getAnimals()
    {
        String animalName[] = new String[1];
        int animalNumber[] = new int[1];
        int lengthOfAnimals;

        animalName[0] = "Null";
        animalNumber[0] = 0;

        while (!animalName[animalName.length - 1].equals("EXTERMINATE"))
        {


            animalName[animalName.length - 1] = JOptionPane.showInputDialog("Name an animal?");
            String numLeft = JOptionPane.showInputDialog("How many are left?");
            animalNumber[animalNumber.length - 1] = Integer.parseInt(numLeft);

            if (animalName[animalName.length - 1].equals("EXTERMINATE"))
            {
                break;
            }

            else
            {

                System.out.println(animalName[animalName.length - 1]);
                animalName = expandName(animalName);
                animalNumber = expandNumber(animalNumber);

            }               
        }




    }

    public static String[] expandName(String animalName[])
    {

        String animalNameExpanded[] = new String[animalName.length + 1];

        for (int a = 0; a <= animalName.length - 1; a++)
        {
            animalNameExpanded[a] =  animalName[a];
        }


        return animalNameExpanded;
    }

    public static int[] expandNumber(int animalNumber[])
    {
        int animalNumberExpanded[] = new int[animalNumber.length + 1];

        for (int a = 0; a <= animalNumber.length - 1; a++)
        {
            animalNumberExpanded[a] =  animalNumber[a];
        }

        return animalNumberExpanded;
    }    

Now when this runs the value that get puts into animalName and animalNumber is now null. What is going on and how do I fix this.

String animalNameExpanded[] = new String[animalName.length + 1];

creates a new array all of whose entries are null . You then copy the values from the old array over, filling all entries but the last one, which remains null .

Add a animalNameExpanded[animalName.length] = "Null"; after the copying, so that

while (!animalName[animalName.length - 1].equals("EXTERMINATE"))

doesn't read a null from the array in the next round.

What @Daniel said, but also this what the Arrays.copyOf(T[], int) method is for. Check the docs for how it handles larger/smaller sizes.

Also, really seems like you want an array of Animal objects, rather than separate arrays for different properties.

Edit

Actually, Daniel's suggestion doesn't seem like great style either. Easier to use a null-safe comparison:

while (!"EXTERMINATE".equals(animalName[animalName.length - 1]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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