• Post Reply Bookmark Topic Watch Topic
  • New Topic

Linking an arraylist and a linkedlist  RSS feed

 
Seb Long
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created a linkedlist of strings with random number values and an arrayList, I want to be able to assign the string rabbit types (aka attributes) to each of the elements in my arrayList to essentially create a virtual set of cards each. I been searching how to do so and I believe the use of a queue may be best, but I don't yet know how this would be implemented.

List rabbits = new LinkedList<>();

   rabbits.add("Beveren");
   rabbits.add("Belgian Hare");
   rabbits.add("Britannia Petite");
   rabbits.add("deilenaar");
   rabbits.add("French Angora");

   Random random = new Random();
   String randomRabbits = rabbits.get(new Random().nextInt(rabbits.size()));

  and

  static final int deck_size = 25;

  ArrayList<Integer> rabbitCards = new ArrayList<Integer>(deck_size);

Many thanks to anyone who is able to assist me. English is not my first language, sorry if you don't understand.
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry, but I don't understand why you have an ArrayList<Integer>. It seems that you propose to put rabbits into this list somehow, but it can only contain integers. I don't understand what those integers are supposed to represent.

And if you want to have a list of cards, then it seems to me that the first step would be to design a Card object. Have you considered that?
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You appear to have a List<String>. It is quite all right to use a Random object to pick a “random” element from that List. You shou‍ld write
List<String> rabbits = new ArrayList<>();
As Paul C says, when you have done the random selection, you will have a String, because that is all the List contains. If you set a capacity for the List, you will have a List which is intended to contain 25 Strings. But you will have to add 25 Strings if you want it to be full. If you try
rabbits.get(20)
you will suffer an Exception.
If you want the List to contain Integers, then that is what it will contain.
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you mixing array lists and linked lists? I am not sure I understand what you mean about linking the Lists to numbers. I suggest you start by drawing diagrams of what you want.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!