ListNode<String> array [duplicate] - java

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);

Related

How to add an item to start of an array in Java [duplicate]

This question already has answers here:
Simplest way to add an item to beginning of an array in Java
(9 answers)
Closed 9 months ago.
I am new to Java and I have an array
int [] A = {140,150,160,170,180,190}
I need to add infinity at the beginning of the array
like so:
int [] A = {inf,140,150,160,170,180,190}
I know from Python to do it like so:
A=[140,150,160,170,180,190]
A=[float('inf')]+A
I need a way to do that in Java as simple as Python without loops
It's important to say I need the array / list(A=[140,150,160,170,180,190]) to be given with the code.
You can use Java ArrayList that implements List interface and has a method add(int,E)
So, you can use something like list.add(0, myObj);.

pass array as argument without first element java [duplicate]

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.

Error with array initialization [duplicate]

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};

why List.contain is returning false [duplicate]

This question already has answers here:
Converting array to list in Java
(24 answers)
Closed 6 years ago.
please see the code below.
int[] intArray={1,2,3,4,3,4,5};
List intList=Arrays.asList(intArray);
System.out.println(intList.contains(1));
Above code is returning false.can any one pls explain why it is so?
Arrays.asList converts your primitive array to a List<int[]> whose single element is the array, intList.contains(intArray) would return true, but intList.contains(1) won't.
If you change your int[] array to Integer[], you'll get your expected output - i.e. a List<Integer> that contains the elements of the original array.

Search array within another array - Algorithm [duplicate]

This question already has answers here:
Find an array inside another larger array
(15 answers)
Closed 8 years ago.
Is there a way to find an array within another array like
a=[1,2,3,4,5,6,7]
b=[2,3,4]
c=[2,4,5]
// b is child of a, but c is NOT child of a.
Well I know that using Brute-force approach I can find the array within another array. But I want to know that is there any algo that can help me ... or (as I am using JAVA so) is there any built-in feature in JAVA that can help me ?
As already mentioned here :
https://stackoverflow.com/a/3940684/351861:
public static int findArray(Integer[] array, Integer[] subArray)
{
return Collections.indexOfSubList(Arrays.asList(array), Arrays.asList(subArray));
}
Java has builting features for that, apparently.

Categories

Resources