I've been learning java, and I have a question about creating an array.

Basically, what I want to do is that I wish to create an array with an unknown array length, then ask the user to input values to be stored in the array. The length of the array depends on the number of integers that the user has entered.

I have tried to find answer by searching online, but failed. Hope you guys can help me out. Thank you!

5 Years
Discussion Span
Last Post by cnlengr

Maybe this would help.

Declare array

int[] someArray;
//Ask user for a size
int size = size_you_got_from_user;
//Allocate array size
someArray = new int[size];

Size could be the number of elements user wants to enter.

Having done that you could loop 'size' times, every time getting the value to be entered into the array.

Alternatively you could use ArrayList. You do not need to give it a size. You could just keep on adding to it.


Edited by kekkaishi: n/a


Thank you, kekkaishi!

I am trying to use a for loop to set the 'size'

import java.util.Scanner;

Scanner input=new Scanner(System.in);
int[] array;
int x;
for(int index=0;index<?? what would be the condition here;index++){
array=new int[x];

But, I found my code does not make sense....Because I will need to have the array created, then pass values to it. Am I right?

Is there a way of creating an array by the time the user enters inputs?


Instead of using array where you have to know size or provide elements from which it have to be created you can use other collections like List, Vector, ArrayList that change their size as you add or remove elements


I think it's best if you take peter's advice. However, since I suggested assigning user specified size to array, I will try to explain the logic.
First, declare the array just the way you did. Next, ask user for the size of the array. For example, you would ask the user 'How many numbers would you like to enter?'. Now the user would give in a number, say 3. Now you'd know that the array would hold 3 numbers and hence the size of the array would be 3. Now you allocate the size to the earlier declared array as demonstrated in the following example.

int[] array;
output: how many numbers would you like to enter. //print to console. you know what to use :)
int size = input.nextInt(); //input is your scanner. 
//Now allocate array with the size
array = new int[size];

Now that you have allocated size to the array, you could start filling it up with values. Use a for loop like below

for(int i=0; i<size; i++){
//ask from user for a number and store it in the array. I am assuming you know this. 

I think I will also demonstrate one way to use ArrayList to achieve the same.

ArrayList<Integer> numbers = new ArrayList<Integer>(); //<Integer> because you are trying to store integer values. 
//Now you can use a while loop to ask for the numbers. And of course  there must be a number to use to indicate when the user is done. 
boolean endLoop = false; //handler to identify if the user is done. 
 while(endLoop == false){ //ask the user to enter numbers as long as endLoop is false
  System.out.println("Enter number: -1 when you are done"); 
  int x = sc.nextInt();
   endLoop = true; //if user enters -1 then it means user would no more want to enter numbers. So we change endLoop to true which would in turn stop the while loop.

You can see that you do not need to worry about the size when you use ArrayList (The same is true for the other collections peter suggested.) Have a look at how you can use ArrayList; it would be way easier.

Hope the above are of use to you.

Edited by kekkaishi: n/a


Thank you all for helping me out there. After studying a bit more on Arraylist and List, I finally got my answer.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.