unchecked conversion on multidimensional arraylist - java

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

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.

Java unchecked conversion of arraylist

Can anybody tell me how to create an array-arraylist the right way ?!
.\File.java:5: warning: [unchecked] unchecked conversion
ArrayList<myObjectType> myParkingLotArray[] = new ArrayList[3];
^
required: ArrayList<Vehicle>[]
found: ArrayList[]
1 warning
I want an arry (or any other solution) which stores 3 arraylists. How to add objects to the arrylists would be nice to know too.
ParentArray
ChildArrayList1
ChildArrayList2
ChildArrayList3
Im happy for any Help
SOLUTION:
public class myClass {
ArrayList<myObjectType>[] myArryName= new ArrayList[3];
public void addLists() {
myArryName[0] = new ArrayList<myObjectType>();
myArryName[1] = new ArrayList<myObjectType>();
myArryName[2] = new ArrayList<myObjectType>();
}
}
The warning can be ignored or suppressed.
You can not create an Array of classes that use generic types - see here!
And there is no way to work around that error message. The compiler tells you: this ain't possible!
Instead - simply stay with one concept. There is no point of mixing arrays and Lists anyway. Just go for
List<List<Vehicle>> parents = new ArrayList<>();
And then
List<Vehicle> someChild = new ArrayList<>();
To finally do something like
parents.add(someChild);
You can do this with a cast
ArrayList<myObjectType>[] myParkingLotArray = (ArrayList<myObjectType>[]) new ArrayList[3];
However, I agree with GhostCat you should try to use arrays or lists but not mix them. a List<List<myObjectype>> would be better.
You cannot create arrays of parameterized types.
What you can do insteade is the following:
List [] arrayOfLists = new ArrayList[10];
arrayOfLists[0] = new ArrayList<Vehicle>();
but you can't be sure that all the lists will be List of the same type.
Otherwise you can use simply List of Lists in this way:
List<List<Vehicle>> listOfLists = new ArrayList<>();
List<Vehicle> list = new ArrayList<>();
listOfLists.add(list);

Array list integer not working

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

Create and iterate a list of Strings in Java

I'm trying to create list of arrays, dynamic in size, add elements and iterate through them when I finish. Currently my code is:
String activities = "SELECT DISTINCT phoneactivity from ContextTable030313";
ArrayList<String> myActivities = new ArrayList();
Cursor cursor2 = db.rawQuery(activities, null);
// looping through all rows and adding to list
if (cursor2.moveToFirst()) {
do {
myActivities.add(cursor.getString(cursor2.getColumnIndex("ts")));
} while (cursor2.moveToNext());
}
Yet it fails to run in the do loop. I believe I am declaring something incorrectly and I get the following warnings:
- ArrayList is a raw type. References to generic type ArrayList<E> should be parameterized
- Avoid object allocations during draw/layout operations (preallocate and reuse instead)
- Type safety: The expression of type ArrayList needs unchecked conversion to conform to
ArrayList<String>
yet I do not understand why this does not work.
The problem seems to be this:
Type safety: The expression of type ArrayList needs unchecked
conversion to conform to
ArrayList
Which points out to this line of code:
ArrayList<String> myActivities = new ArrayList();
You should change that line to this:
ArrayList<String> myActivities = new ArrayList<String>();
On a different note, you could replace this code segment:
if (cursor2.moveToFirst()) {
do {
myActivities.add(cursor.getString(cursor2.getColumnIndex("ts")));
} while (cursor2.moveToNext());
}
With this:
while (cursor2.moveToFirst()) {
myActivities.add(cursor.getString(cursor2.getColumnIndex("ts")));
}
change your arrayList declaration such that it accepts parameterized type during constructor invokation.
ArrayList<String> myActivities = new ArrayList<String>();
If you use java 7 or above:
ArrayList<String> myActivities = new ArrayList<>();
You don't need to declare the parametreized type during constructor invokation due to *Type inference*but you still need to declare empty <>.

Java 1.6: Creating an array of List<T>

Why can't I create an array of List ?
List<String>[] nav = new List<String>[] { new ArrayList<String>() };
Eclipse says "Cannot create a generic array of List"
or
ArrayList<String>[] nav = new ArrayList<String>[] { new ArrayList<String>() };
Eclipse says "Cannot create a generic array of ArrayList"
or
List<String>[] getListsOfStrings() {
List<String> groupA = new ArrayList<String>();
List<String> groupB = new ArrayList<String>();
return new List<String>[] { groupA, groupB };
}
But I can do this:
List[] getLists() {
return new List[] { new ArrayList(), new ArrayList() };
}
Eclipse says that List and ArrayList are raw types but it compiles...
Seems pretty simple, why won't it work?
Well, generics tutorial give the answer to your question.
The component type of an array object
may not be a type variable or a
parameterized type, unless it is an
(unbounded) wildcard type.You can
declare array types whose element type
is a type variable or a parameterized
type, but not array objects.
This is
annoying, to be sure. This restriction
is necessary to avoid situations like:
// Not really allowed.
List<String>[] lsa = new List<String>[10];
Object o = lsa;
Object[] oa = (Object[]) o;
List<Integer> li = new ArrayList<Integer>();
li.add(new Integer(3));
// Unsound, but passes run time store check
oa[1] = li;
// Run-time error: ClassCastException.
String s = lsa[1].get(0);
If arrays of parameterized type were
allowed, the previous example would
compile without any unchecked
warnings, and yet fail at run-time.
We've had type-safety as a primary
design goal of generics.
You can't create arrays of generic types, generally.
The reason is that the JVM has no way to check that only the right objects are put into it (with ArrayStoreExceptions), since the difference between List<String> and List<Integer> are nonexistent at runtime.
Of course, you can trick the compiler by using the raw type List or the unbound wildcard type List<?>, and then cast it (with a unchecked cast) to List<String>. But then it is your responsibility to put only List<String> in it and no other lists.
No exact answer, but a tip:
Last example has a raw type warning because you omitted the typization of the list; it is generally a better (type safe) approach to specify which object types are contained in the list, which you already did in the previous examples (List<String> instead of List).
Using arrays is not best practice, since their use contains errors most times; Using Collection classes (List, Set, Map,...) enables use of typization and of convenient methods for handling their content; just take a look at the static methods of the Collections class.
Thus, just use the example of the previous answer.
Another solution is to extend LinkedList<String> (or ArrayList<String>, etc.), then create an array of the subclass.
private static class StringList extends LinkedList<String> {}
public static void main(String[] args)
{
StringList[] strings = new StringList[2];
strings[0] = new StringList();
strings[1] = new StringList();
strings[0].add("Test 1");
strings[0].add("Test 2");
strings[1].add("Test 3");
strings[1].add("Test 4");
System.out.println(strings[0]);
System.out.println(strings[1]);
}

Categories