0

Does a Java Array have to be assigned a length in the beginning? or can it be dynamic and accept unlimited amounts of data until the user decides that is enough?

Everywhere I look the length is always defined... example

public static void main(String[] args) 
{
int a[] = new int[10];

System.out.println("Please enter your number: "\n);

}

Instead of having 10, is it possible to say at a time in the future when the user decides by entering a certain number, like -1, the array is complete? Therefore, a user can continue to input information because they do not know how much information they will enter into the array. I did find in one place using (int ... number), is this a viable way of declaring no declared amount?

1
  • 2
    So you're looking for an ArrayList? Commented Apr 4, 2014 at 19:03

4 Answers 4

2

Java arrays have a fixed length. If you want something which can grow as needed, I would suggest using an ArrayList, which is backed by an array, and does all the hard work for you.

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

6 Comments

in addition, ArrayList allows use of the advanced for-each loop. You should use the List interface as your variable type so that you have code flexibility to change ArrayList to another type of list if you choose with no code modifications needed. so like: List<Integer> a = new ArrayList<Integer>();
@azurefrog You said using an ArrayList backed by an Array but once again the Array has a limit or can you use ... ?
@Paul Skinner - Yes, but the array list keeps track of how full its backing array is, and will create a new (larger) array and copy the old data over when needed.
@PaulSkinner what he means by "backed by an array" is that under-the-hood, ArrayList has an internal, private array which holds your data. This array is always 10 elements larger than your data. When you add to it, the ArrayList is smart and will auto-expand it'self by copying your array to a new, larger array (all internally).
@Paul Skinner - For this reason, if you know before hand that you are going to want to store a large amount of data, you can create the ArrayList with a larger backing array than the default 10 elements. That will reduce unnecessary copying of the data as you add it.
|
0

In Java the Array is a fixed length data structure.So you can not change length of Array once created.You can create an array by giving any integer as its size(positive or negative integer).If you give negative integer then it will compiles fine but you will get NegativeArraySizeException exception at run time.

Accroding to your needs I will suggest ArrayList.
Here is the difference between ArrayList and Array

Comments

0

Yes, you must specify size for java array. This is one main difference between java array and list. The array list grows dynamically.. You can refer here...

http://java67.blogspot.in/2012/12/difference-between-array-vs-arraylist-java.html?m=1

Comments

0

You can use ArrayList, something like this:

    public static void main(String args[]) {
        List<Integer> a = new ArrayList<Integer>();
        a.add(1);
    }

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.