9

Am new and I've searched the web and found it a bit frustrating, all I got was that in java array's can't be re-sized. I just want to figure out How do I create an empty array where I then prompt the user to enter numbers to fill the array? I don't want to define the capacity of the area like int[] myArray = new int[5]; I want to define an empty array for which a user defines the capacity, example- they enter number after number and then enter the word end to end the program, then all the numbers they entered would be added to the empty array and the amount of numbers would be the capacity of the array.

3
  • 1
    An empty array is an array with no elements. For non-empty arrays, elements are initialized to their default value. Commented Apr 14, 2014 at 18:40
  • Read user input into a variable and use its value to initialize the array. myArray = new int[someVarSetEarlier] if you want to get the size in advance or use a List if you want the length of the collection to be changed dynamically. Commented Apr 14, 2014 at 18:41
  • Use ArrayList<Integer> instead Commented Apr 14, 2014 at 18:41

6 Answers 6

10

You cannot make an empty array and then let it grow dynamically whenever the user enters a number in the command line. You should read the numbers and put them in an ArrayList instead. An ArrayList does not require a initial size and does grow dynamically. Something like this:

public void main(String[] args) {
    ArrayList<Integer> numbers = new ArrayList<Integer>();
    BufferedReader console = 
            new BufferedReader(new InputStreamReader(System.in));
    while(true) {
        String word = console.readLine();
        if (word.equalsIgnoreCase("end") {
            break;
        } else {
            numbers.add(Integer.parseInt(word);
        }
    }

Ofcourse you won't use while(true)and you won't put this in main, but it's just for the sake of the example

Sign up to request clarification or add additional context in comments.

Comments

6

How about:

Scanner scan = new Scanner(System.in);
System.out.print("Enter the array size: ");
int size = scan.nextInt();
int[] yourArray = new int[size];
//can even initialize it
Arrays.fill(yourArray, -1);

Comments

1

try ArrayList, it's a dynamic array http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

2 Comments

You should attempt to expand upon your answer, and not just include a link
sorry i'm new here @Rogue
1
    import java.util.Scanner;


public class Ex {
public static void main(String args[]){
    System.out.println("Enter array size value");
    Scanner scanner = new Scanner(System.in);
    int size = scanner.nextInt();
    int[] myArray = new int[size];
    System.out.println("Enter array values");
    for(int i=0;i<myArray.length;i++){
        myArray[i]=scanner.nextInt();
    }
    System.out.println("Print array values");
    for(int i=0;i<myArray.length;i++){
        System.out.println("myArray"+"["+i+"]"+"="+myArray[i]);
    }
}
}

Comments

0

Take n from the user input and:

int a [] = new int [n];

Comments

0

Since you are using for integer, can try using ArrayList with following example:

ArrayList<Integer> integerListArray = new ArrayList<Integer>();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.