ArrayList<ArrayList<int>> compiler problem

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2008
Posts: 64
Reputation: chunalt787 is an unknown quantity at this point 
Solved Threads: 1
chunalt787 chunalt787 is offline Offline
Junior Poster in Training

ArrayList<ArrayList<int>> compiler problem

 
0
  #1
Oct 27th, 2009
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:
  1. void addToList(int[] a, int j, int i, int N)
  2. {
  3. ArrayList< ArrayList<int> > perms = new ArrayList<ArrayList<int>>();
  4. for(int x = 0; x < N; x++) {
  5. perms.add(new ArrayList<int>());
  6. perms.get(perms.size()-1).add(a[x]);
  7. System.out.print(a[x]);
  8. }
  9. System.out.println(" swapped(" + j + ", " + i + ")");
  10. } // 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!
Last edited by chunalt787; Oct 27th, 2009 at 2:37 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,024
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 151
JamesCherrill JamesCherrill is online now Online
Veteran Poster
 
1
  #2
Oct 27th, 2009
It's annoying, but you can't use a primitive type (int/char/boolean etc) with generics.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 64
Reputation: chunalt787 is an unknown quantity at this point 
Solved Threads: 1
chunalt787 chunalt787 is offline Offline
Junior Poster in Training
 
0
  #3
Oct 27th, 2009
So I would have to make a wrapper class myInt which stores an int as its data member?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,511
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster
 
1
  #4
Oct 27th, 2009
There are existing wrapper classes for all of the primitive types.
Use Integer. Auto-boxing will handle the casting for you.
Last edited by Ezzaral; Oct 27th, 2009 at 4:07 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 64
Reputation: chunalt787 is an unknown quantity at this point 
Solved Threads: 1
chunalt787 chunalt787 is offline Offline
Junior Poster in Training
 
0
  #5
Oct 27th, 2009
Ok thank you
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 442 | Replies: 4
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC