Why this piece of code does not work? How to set the length of Object[].
Object[] entry = new Object[]{};
entry[0] = 1;
entry[1] = "1";
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you are Initializing a new array then you either have to specify its size explicitly using the syntax mentioned in the answer above:
or use the array initializer block what you presented in your question like this:
What you did here was initializing an array to be empty thus setting its size to 0. If you print out the length of that array it will be 0.
If you want an array whose size can be modified you are advised to use the List interface (an ArrayList for example).
You can set the initial capacity of an ArrayList using its constructor:
List is generic so you should use the type parameter otherwise you will get some warnings.
If you don’t know what objects will you store in your List you may do something like this:
but you will get warnings that way.
More info about arrays: Arrays
And about Lists: List interface
If you are interested in java Generics: Generics