Array error in java [duplicate] - java

This question already has answers here:
How is this illegal
(5 answers)
Closed 5 years ago.
int[][][] inputs;
inputs = new int[10][][];
inputs[0] = new int[1][];
inputs[0][0] = new int[14]{1,1,-1,-1,-1,1,-1,-1,1,-1,-1,-1,1,1};
This is an excerpt from my program, I have no idea why this is causing an error. Isn't this correct?
Thanks in advance :-)

In Eclipse I get a pretty clear error message:
Cannot define dimension expressions when an array initializer is provided.
That means that you can either specify the dimension or the array initializer (i.e. the values). You can't specify both at the same time.
Simply change your last line to
inputs[0][0] = new int[]{1,1,-1,-1,-1,1,-1,-1,1,-1,-1,-1,1,1};

You cannot construct an array with a declared length AND a static initialiser. It must be either one or the other.
change inputs[0][0] = new int[14]{1,1,-1,-1,-1,1,-1,-1,1,-1,-1,-1,1,1}; to inputs[0][0] = new int[]{1,1,-1,-1,-1,1,-1,-1,1,-1,-1,-1,1,1}; - the length of the new array is implicit because you are explicitly initialising the array with 14 elements.

The last line should simply be:
inputs[0][0] = {1,1, etc.};

Related

error: generic array creation when trying to create an array of JComboBoxes [duplicate]

This question already has answers here:
Array of Generic List
(5 answers)
Error generic array creation
(3 answers)
Closed 8 months ago.
I have a 4 x 6 grid (24 elements) of dropdown boxes in a GUI that all contain the same dropdown list, but evidently each can be selected to a different value.
Trying to generalise this I want to use an array of JComboBox[4][6], so the straightforward way to declare this before iteratively filling the comboboxes would be something like this:
int nLines = 4;
int nParams = 6;
JComboBox<String>[][] labelFields = new JComboBox[nLines][nParams];
Yet this produces a warning:
Gui.java:40: warning: [unchecked] unchecked conversion
JComboBox<String>[][] labelFields = new JComboBox[nLines][nParams];
Ignoring the warning and trying to fill the JComboBoxes iteratively at runtime doing something like this blows up with a NullPointerException:
labelFields[0][0].addItem("Some string");
So let's try to make the warning go away, again in the most straightforward way:
JComboBox<String>[][] labelFields = new JComboBox<String>[nLines][nParams];
But, disappointment ensues at compile time:
Gui.java:40: error: generic array creation
JComboBox<String>[][] labelFields = new JComboBox<String>[nLines][nParams];
^
What am I missing here and what detours must this tired old mule take to make this work?

String Array Assignment in Java [duplicate]

This question already has answers here:
Arrays constants can only be used in initializers error
(5 answers)
Closed 4 years ago.
Code:
String Foo[];
Foo={"foo","Foo"};
Error at Line 2: Illegal Start of expression
The code works if I say:
String Foo[]={"foo","Foo"};
Why does this happen, and how should I do the required without generating an error? This also happens with other data types.
Would appreciate if you could explain in layman terms.
{"foo","Foo"} is an array initializer and it isn't a complete array creation expression:
An array initializer may be specified in a declaration (§8.3, §9.3, §14.4), or as part of an array creation expression (§15.10), to create an array and provide some initial values.
Java Specification
Use new String[] {"foo","Foo"} instead.
You have to initialize the string array:
String foo[] = new String[]{"foo, "Foo"}; Or
String foo[] = {"foo, "Foo"};
Modern IDEs give error for not initializing the array objects. You can refer more details here:
http://grails.asia/java-string-array-declaration

Is it necessary to specify the size of an array in java? [duplicate]

This question already has answers here:
creating array without declaring the size - java
(6 answers)
Closed 5 years ago.
Is it necessary to specify the size of an array in java
Like is it necessary to every time do this
int arr[]=new int[n];
to specify the size is it necessary!
(Don't consider ArrayList please!)
You have 3 possibilities:
specify size:
int[] array = new int[n];
specify size indirectly:
int[] array = {1,2,3,67,345};
use ArrayList with open size:
ArrayList<Integer> list = new ArrayList<Integer>();
If you want to have a list with a changeable size, I advise you to use ArrayList.
In a word - yes. You must specify the size of an array when you initialize it. Note that there's also a syntax to initialize an array from it's elements (e.g. new int[]{1, 2, 3}) which doesn't explicitly require the size, but deduces it from the number of elements you provided.

Assigning space for List<List<MyObj>>

I currently am trying to understand how Java assigns space when I ask it to.
Specifically (and as I am aware that I'm unable to declare an array with generics) I made this assignment:
List<List<Visitor>> elevlists;
elevlists = new ArrayList<List<Visitor>>(5);
as I want elevlists to be a list of 5 lists of Visitors. To my understanding this command will clear out space for five lists of Visitors and not have them there.
So I added this, which (and I underline) doesn't work:
List<Visitor> (elevlists.get(0)) = new ArrayList<Visitor>();
to see if I can actually create an instance for any of the lists.
But after some tweaking I ended to this code:
List<List<Visitor>> elevlists;
elevlists = new ArrayList<List<Visitor>>(5);
List<Visitor> temp = (elevlists.get(0));
temp = new ArrayList<Visitor>();
which, to my surprise, seems okay to the compiler.
Any help on why this is acceptable when the code above it isn't?
Thanks a lot.
ArrayList is not a array. It's a wrapper for array.
List<List<Visitor>> elevlists;
elevlists = new ArrayList<List<Visitor>>(5);
Here you just create a object ArrayList with 5 elements. But all elements inside this array is NULL.
Proper code should be:
List<List<Visitor>> elevlists;
elevlists = new ArrayList<List<Visitor>>(5);
List<Visitor> temp = new ArrayList<Visitor>();
elevlists.add(temp);
This is not doing what you think it is.
List<Visitor> temp = (elevlists.get(0));
temp = new ArrayList<Visitor>();
The second line affects only the variable temp, which now points to a newly allocated ArrayList.
Read this as
get the value stored at element zero (which is currently null) in elevlists, assign that value to temp.
now throw away that value in temp, and instead make temp point to a new Array List
If you want to make elevlists have some non-null values use
elevlists.add(temp);
I think, although your second code will compile, it will fail at runtime because the elevlists = new ArrayList<List<Visitor>>(5); does not actually create elements in elevLists - the 5 is just a hint to the ArrayList class how much space to reserve. ArrayList is an array wrapper and doesn't reserve memory for an array in the same way as, say, C.
#Tom is right in comments - the first example doesn't compile because you can't have method calls on the LHS of assignments.

Java - NullPointerException in Array [duplicate]

This question already has answers here:
NullPointerException when Creating an Array of objects [duplicate]
(9 answers)
Closed 9 months ago.
I have encountered the following problem: I have a java class with a private member like so:
private Arcs[] arcs;
This is not initialised in the constructor because I don't know the length of my vector yet, but it is initialised in the read function, where I read the info from a file.
In this function I do the following:
arcs = new Arcs[n]; //n is a number read from file
Then there is a while cycle in which I read other stuff from the file and I have something like:
while(condition){
...
arcs[i].add(blah); //i is a valid number, smaller than n, and the add function is also correct
...
}
But here I have an error saying NullPointerException and I don't understand why. I would appreciate it, if someone would explain to me what's happening.
Are you actually ever storing an Arcs object in arcs[i]? If not, all elements of arcs[] will be initialized to null. (Hence the NPE)
Do something like this:
while(condition){
// ...
arcs[i] = new Arcs();
arcs[i].add(blah);
// ...
}
Reference:
Java Tutorial: Arrays

Categories