This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

LinkedList

 
Nick Stevens
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm trying to make a hash table that will store
many sets of four integers. As part of this
I thought I could store each set of integers
in an 4 x 1 integer array and then store the
arrays in a linked list. I've added arrays
okay using the LinkedList class but I can't work
out how to access the arrays once they are in
the linked list. Here's some of the code:

int[] arrayOne = new int[4];
int[] arrayTwo = new int[4];

List aList = new LinkedList();

aList.add(arrayOne);
aList.add(arrayTwo);

int listIndex = aList.indexof(arrayTwo);

// This line doesn't work:
int[] myArray = (int[])aList.get(listIndex);


Any help would be much appreciated!
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by "this line doesn't work"? Do you get compiler errors? If so, what are the errors? If not, does it run? What output do you get and how does it differ from what you expect?

At a glance, the syntax looks correct, so you need to answer these questions in order for us to help you. Please provide as much information about the exact problem as you can.

Layne
 
Nick Stevens
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your reply - you're right it *does* work!
I feel stupid now. I tried lots of other things before I
tried that last line and my code was a lot messier with many
extra lines and I didn't test the last line properly. Ahem.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nick Stevens:
I tried lots of other things before I
tried that last line and my code was a lot messier with many
extra lines and I didn't test the last line properly. Ahem.
Yellow Card for Nick!

To clear it, please choose another post in this forum (or any other) and try to help the poster with their problem. Then it will be cleared.
 
Nick Stevens
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A yellow card. Doh! How will I know when I've posted a reply that is deemed sufficiently useful?
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nick Stevens:
How will I know when I've posted a reply that is deemed sufficiently useful?
Only you can be the judge of that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic