This question already has answers here:
How is this illegal
(5 answers)
Array error in java [duplicate]
(3 answers)
Error in simple array... How to fix [duplicate]
(1 answer)
Closed 5 years ago.
New to Java, this is part of a practice question from a question in my book where i'm learning java. I think I've traced the error to the array, in particular to the initialization but i'm not sure how to fix it, why isn this correct?
int[][][] arr;
arr= new int[20][][];
arr[0] = new int[1][];
arr[0][0] = new int[10]{1,1,-1,-1,-1,1,-1,-1,1,-1};
When you supply an array initializer expression, you can't specify the array dimensions too.
You can use :
arr[0][0] = new int[]{1,1,-1,-1,-1,1,-1,-1,1,-1};
When i run it in eclipse it says Cannot define dimension expressions when an array initializer is provided., i think that's really clear for an error message. It means you can either specify the dimensions or the initialize the array. But NOT both at the same time.
Change to:
inputs[0][0] = new int[]{1,1,-1,-1,-1,1,-1,-1,1,-1};
Related
This question already has answers here:
Syntax for creating a two-dimensional array in Java
(13 answers)
Closed 3 years ago.
I want to create a character array like this one below:
character array
i am unable to find a method . Please help me out. Beginner in java
char[][] x = new char[][]{};
x[0][1] = 'c';
...
This question already has answers here:
How to use subList()
(6 answers)
Closed 3 years ago.
I want to pass an array as argument to a function without first element.
I came up with this solution, but I'm wondering if there are better ways to it.
List<Integer> numbers = new ArrayList<>();
//... in the meantime numbers is an array that contain 1000 elements;
numbers.remove(0);
myFunction(numbers)
You can use subList(firstElement, lastElement); method.
Here please check javadoc.
This question already has answers here:
How do I declare and initialize an array in Java?
(31 answers)
Closed 5 years ago.
I am trying to declare and instantiate an array named 'canines' that had 25 elements and will hold 'dog' objects...
Is this the best way to do this or is there another formate that would be more correct?
canines[25] = Dog
Dog[] canines = new Dog[25];
Have a look at some of the many online java resources
This question already has answers here:
Java 1.6: Creating an array of List<T>
(4 answers)
Closed 7 years ago.
I'm trying to create an array of ListNode<String> in Java.
I get this error: "cannot create a generic array of ListNode<String> [] when I try to create the array as such:
ListNode<String> [] array = new ListNode<String> [4];
But when I create it like this:
ListNode<String> [] array;
I get an error further down when I try to set spots in the array. For example:
array[0]=start;
array[1]=start2;
Gives me the error "The local variable array may not have been initialized."
How do I fix this? Sorry if this is a basic question! I am in high school.
You can't initialize a ListNode<String>[] because Java simply doesn't allow generic arrays. In your second snippet, you don't initialize the array, so you can't use it.
To solve this, you'll want to use a list instead of an array:
List<ListNode<String>> list = new ArrayList<>();
//....
list.add(start);
list.add(start2);
This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 8 years ago.
I'm new to Java but not new to programming. I ran the following code in Eclipse:
int[] intArray = new int[5];
System.out.println(intArray);
and received the following output:
[I#17f7be7b
I'm sure StackOverflow already contains the correct way to create an array in Java. What I'd like to know is: what did I do?
Edit: Sorry... The link above my post isn't a duplicate question, and it doesn't answer my question. TylerAndFriends' link is closer, but I was hoping for an explanation of exactly what I printed. Tyler's linked thread says "the default method is to display the object's class name representation, then "#" followed by its hashcode". Can someone elaborate?
Use this
for (int i : intArray) {
System.out.println(i);
}