No_name
No_name

Reputation: 2810

Dynamic Array Algorithm

As part of a homework assignment, we are supposed to create an array that will resize itself if the user attempts to input more data to a new index that is out of bounds. We are not allowed to use any libraries like hashsets, arraylists, etc. My code works, however, the length of the array always ends up being one larger than what is required. I know the problem lies in the nature of the while loop because it will grow and then add, but I don't know how I would fix it.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
public class DynamicArray
{
    public static void main(String[] args)
    {
        Scanner kb = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        System.out.print("Enter a desired length for an array: ");
        String[] x = new String[kb.nextInt()];
        int index = 0;
        System.out.print("Enter as many Strings as desired, separated by a new line. Type in \"end\" to print out the contents of the array.");
        String input = kb.nextLine();
        while(!input.equalsIgnoreCase("end"))
        {
            if (index < x.length)
            {
                x[index] = input;
            }
            else
            {
                String[] temp = new String[x.length + 1];
                for (int i = 0; i < x.length; ++i)
                {
                    temp[i] = x[i];
                }
                temp[index] = input;
                x = temp;
            }
            ++index;
            input = kb.nextLine();
        }
        for (int i = 0; i < x.length; ++i)
        {
            System.out.println(x[i]);
        }
    System.out.println(x.length);
    }
}    

Upvotes: 1

Views: 667

Answers (1)

ruakh
ruakh

Reputation: 183300

I know the problem lies in the nature of the while loop because it will grow and then add […]

Not at all. The problem lies in the way Scanner.nextInt() and Scanner.nextLine() work. Scanner.nextInt() will read in an integer, but will not swallow the newline after the integer. So the first thing Scanner.nextLine() sees is that newline, and it thinks it sees an empty line, which is what it returns. So x[0] is a the empty string.

You can see this a bit more clearly if you change this:

            System.out.println(x[i]);

to this:

            System.out.println(i + ": " + x[i]);

because then you'll see that the first thing it prints is 0:.

By the way, your approach in general is very inefficient, since it requires creating many more arrays than are actually needed. Instead of increasing the size of the array by one, it's much more efficient to double the size of the array, and keep track of its length separately (rather than using x.length). (Admittedly, in your case, efficiency is probably a non-issue, since you're taking input from the user, and there's no way that the user will type in elements anywhere near as fast as Java can copy the array; but in general, that's the best way to design a dynamically-resizeable array.)

Upvotes: 3

Related Questions