943,946 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 5998
  • Java RSS
Oct 27th, 2009
0

ArrayList<ArrayList<int>> compiler problem

Expand Post »
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:
Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 39
Solved Threads: 1
Junior Poster in Training
chunalt787 is offline Offline
84 posts
since Apr 2008
Oct 27th, 2009
1
Re: ArrayList<ArrayList<int>> compiler problem
It's annoying, but you can't use a primitive type (int/char/boolean etc) with generics.
Featured Poster
Reputation Points: 1924
Solved Threads: 952
Posting Expert
JamesCherrill is offline Offline
5,788 posts
since Apr 2008
Oct 27th, 2009
0
Re: ArrayList<ArrayList<int>> compiler problem
So I would have to make a wrapper class myInt which stores an int as its data member?
Reputation Points: 39
Solved Threads: 1
Junior Poster in Training
chunalt787 is offline Offline
84 posts
since Apr 2008
Oct 27th, 2009
1
Re: ArrayList<ArrayList<int>> compiler problem
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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Oct 27th, 2009
0
Re: ArrayList<ArrayList<int>> compiler problem
Ok thank you
Reputation Points: 39
Solved Threads: 1
Junior Poster in Training
chunalt787 is offline Offline
84 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Jlist Renderer
Next Thread in Java Forum Timeline: LAN Video Conference System





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC