Member Avatar for chunalt787

I am trying to create a 2d array list but it is giving me a compiler error pointing at the first line of the function. I don't really get what is causing the problem. I am pretty good in C++ but Java has some new rules that I am trying to learn. Does anyone see a problem here?

Code:

void addToList(int[] a, int j, int i, int N) { ArrayList< ArrayList<int> > perms = new ArrayList<ArrayList<int>>(); for(int x = 0; x < N; x++) { perms.add(new ArrayList<int>()); perms.get(perms.size()-1).add(a[x]); System.out.print(a[x]); } System.out.println(" swapped(" + j + ", " + i + ")"); } // addToList()

Error:

MiniMaxTree.java:119: unexpected type found : int required: reference ArrayList< ArrayList<int> > perms = new ArrayList<ArrayList<int>>(); ^ MiniMaxTree.java:119: unexpected type found : int required: reference ArrayList< ArrayList<int> > perms = new ArrayList<ArrayList<int>>();

Thanks in advance!

Member Avatar for JamesCherrill

It's annoying, but you can't use a primitive type (int/char/boolean etc) with generics.

Member Avatar for chunalt787

So I would have to make a wrapper class myInt which stores an int as its data member?

Member Avatar for Ezzaral

There are existing wrapper classes for all of the primitive types.
Use Integer. Auto-boxing will handle the casting for you.

Member Avatar for chunalt787

Ok thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.