Array list integer not working - java

i am trying to make an ArrayList of type integer, but it gives me this error(I am using this compiler called jikes)
Code:
ArrayList<Integer> = new ArrayList<Integer>();
Error:
***Semantic error: using type arguments to access generic types requires the use of "-source 1.5"'or greater. Compilation will continue to use the raw type "Java.util.arraylist", but no class file will be emitted.

Your arraylist has no name:
ArrayList<Integer> name = new ArrayList<>();

try following:
List<Integer> list = new ArrayList<Integer>();

Correct initialization should be
ArrayList<Integer> X = new ArrayList<Integer>();
you need to assign variable

Related

Storing an ArrayList in an 2d Array Java

How can I store an ArrayList in a two dimensional array?
I've tried it like this, but it won't work:
ArrayList<Integer> arrList = new ArrayList<Integer>();
ArrayList<Integer>[][] arr = new ArrayList<Integer>[9][9];
but it won't even let me declare the ArrayList-Array.
Is there a way to store a list in a 2d array?
Thanks in advance!
You can't create arrays of generic types in Java. But this compiles:
ArrayList<Integer> arrList = new ArrayList<Integer>();
ArrayList<Integer>[][] arr = (ArrayList<Integer>[][]) new ArrayList[9][9];
arr[0][0] = arrList;
Why can't you create these arrays? According to the Generics FAQ, because of this problem:
Pair<Integer,Integer>[] intPairArr = new Pair<Integer,Integer>[10]; // illegal
Object[] objArr = intPairArr;
objArr[0] = new Pair<String,String>("",""); // should fail, but would succeed
Assuming you want an ArrayList inside an ArrayList inside yet another ArrayList, you can simply specify that in your type declaration:
ArrayList<ArrayList<ArrayList<Integer>>> foo = new ArrayList<ArrayList<ArrayList<Integer>>>();
Entries can be accessed via:
Integer myInt = foo.get(1).get(2).get(3);
Just be wary of boundaries - if you try to access an out of bounds index you'll see Exceptions thrown.

Converting Object[] to ArrayList<V>[] in Java

The following code throws an ClassCastException:
ArrayList<V>[] table = (ArrayList<V>[]) new Object[tableSize];
How can I create an Array of ArrayList if this doesn't work?
EDIT:
To be shure everybody understands the problem. I need an Array that contains multiple ArrayLists.
EDIT2:
tableSize is an int.
EDIT: OK, here is what you can do:
ArrayList<String>[] table = new ArrayList[tableSize];
It compiles and works on Java 8 i'm using.
In case of Java you can not create new instance of array that type is generic.
T[] array = new T[size]; - cause compile error Cannot create a generic array of T.
The same error will be present when you will try to use it
ArrayList<T>[] array = new ArrayList<T>[size];
With the same reason. The Java runtime does not know what is T after compilation therefor will not be able to allocate valid memory space.
To pass this issue you have following options:
Remove the generic type and use a raw.
ArrayList[] array =new ArrayList[size];
Use list of a list
List<List<T>> list = new ArrayList<List<T>>();
Not the smoothest way to do it But if I got your question correctly something like this should do the trick:
int tableSize = 2;
Collection<String> listWithItems = new ArrayList<>();
listWithItems.add("Foo");
listWithItems.add("Bar");
Collection<Collection<String>> listOfList = new ArrayList<>();
listOfList.add(listWithItems);
Object[] array = listOfList.toArray(new Object[tableSize]);
System.out.println("Col: " + listOfList);
System.out.println("Array: " + Arrays.toString(array));
Print out is:
Col: [[Foo, Bar]]
Array: [[Foo, Bar], null]
Short example without extra "fluff" to get the inital array:
Object[] theArrayOfLists = new ArrayList<List<SomeClass>>().toArray(new Object[tableSize]);

Creating a new ArrayList

i have a question about creating a new array list.
If i create a new one, with
ArrayList <?> listtwo = new ArrayList<?>();
What can i put into the array? Can i put Strings and Integers in it?
Or how does this work.
I know that if you create an list like :
ArrayList<String> list =new ArrayList<String>();
that you can only put Strings in it. and if u try to do something else it gives a compile exception.
If i create a new one, with
ArrayList <?> listtwo = new ArrayList<?>();
You can't. You can't instantiate a parameterized type without bounds. Here's what the compiler will say:
ArrayList <?> listtwo = new ArrayList<?>();
^
required: class or interface without bounds
found: ?
So the question of what you can put in it is moot.
If you didn't supply a type parameter at all:
ArrayList listtwo = new ArrayList();
...you could put anything you liked into it (including a mix of things). Primitive values will get coerced to their object equivalents (int => Integer, etc.). At that point, it's basically an ArrayList<Object> instance.
There are two issues here.
(a) new ArrayList<?> is not a legal expression.
If you want to create a list that can hold String and Integers you need to use ArrayList<Object> as Object is the (most specific) common super-type of String and Integer:
ArrayList<Object> list = new ArrayList<Object>;
list.add('abcd'); // insert a string
list.add(1234); // insert an integer
The down side is that when you extract an item from this list it will be of type Object so you will need to downcast it:
String s = (String) list.get(0);
Integer n = (Integet) list.get(1);
(b) Regarding the wildcard ? symbol:
It can be used for specifying the type of a variable, as in: ArrayList<?> list = ... but it cannot be used in a new expression. In other words, this expression is illegal: new ArrayList<?>.
The wildcard is useful for allowing (limited) polymorphism between lists. Specifically, if you have a List of Integers and you want to treat it like a list of Number (which makes sense as Integer is a subclass of Number) you can use the wildcard symbol.
ArrayList<Integer> list2 = new ArrayList<Integer>();
list2.add(100);
list2.add(200);
// This assignment is OK:
ArrayList<? extends Number> list3 = list2;
Number n = list3.get(0); // n is now 100
However, the problem with list3 (or any other list with a wildcard type) is that you cannot mutate it. For instance, if you try to add an element to this list:
list3.add(300); // <-- This is not allowed
you will get a compile-time error.

unchecked conversion on multidimensional arraylist

Does anyone know how to fix this warning?
MyMain.java:12: warning: [unchecked] unchecked conversion
found : java.util.ArrayList[][]
required: java.util.ArrayList<java.lang.String>[][]
obj[count].someArrayList = new ArrayList[4][4];
I tried to change this to:
obj[count].someArrayList = new ArrayList<String>[4][4];
But the amendment changes the warning into the following error:
MyMain.java:12: generic array creation
obj[count].someArrayList = new ArrayList<String>[4][4];
The declaration of someArrayList is:
public ArrayList<String>[][] someArrayList;
You cannot create arrays with generics (hence the error). The warning indicates the heavily discouraged use of raw types with an ArrayList.
Instead, the following creates a multidimensional ArrayList:
public ArrayList<ArrayList<String>> someArrayList;
You can then perform normal operations on your multidimensional ArrayList.
obj[count].someArrayList = new ArrayList<ArrayList<String>>();
ArrayList<String> toAdd = new ArrayList<String>();
toAdd.add("test");
toAdd.add("test2");
obj[count].someArrayList.add(toAdd);
Only way to declare a multidimensional array of ArrayList is like this:
#SuppressWarnings("unchecked")
ArrayList<String>[][] someArrayList = new ArrayList[4][4];
someArrayList[0][0] = new ArrayList<String>();
someArrayList[0][1] = new ArrayList<String>();
....
....

Java Collection<Object> or Collection<?>

I try use List instead of List
List<?> list = new ArrayList<Integer>();
...
list.add(1); //compile error
What i do wrong and how cast new value to Integer?
Maybe i shoud use List in my project?
List<?> means a list of some type, but we don't know which type. You can only put objects of the right type into a list - but since you don't know the type, you in effect can't put anything in such a list (other than null).
There is no way around this, other than declaring your variable as List<Integer>.
List<Integer> list = new ArrayList<Integer>();
The generic type always has to be the same.
List<Integer> list = new ArrayList<Integer>();
...
list.add(1);
List<?> list = new ArrayList<Integer>();
? means some type, you have to specify the Type as Integer. so the correct syntax would be :
List<Integer> list = new ArrayList<Integer>();

Categories