1

I'm trying to generate a variable number of arrayLists with a variable number of items within the list. This means that I can't use an array instead of an arrayList.

When generating a JLabel in an array, you use the for loop and say:

 label[i]=new JLabel();

Is this possible for an arrayList?

I can't put up my code because it's to long but i'll give you the general idea: Within a txt, there are multiple objects, in this case, troops: longbowman, man at arms, knight... Each 1 shares the same stats (ArrayLists) but have different values for each one. I have it so that once the bufferedReader finishes with a troop, it sends all the arraylists to another class, and continues reading and overwriting the old troops stats (they are now stored in a different class). the problem is, how can the other class differentiate between the stats? This would be easy if the number of types of troops was constant, but, it isn't. So how can I do this?

Edit: here is a bit of the source code:

while ((text = reader.readLine()) != null) {
            StringTokenizer troops = new StringTokenizer(text, "=");
            String list = troops.nextToken();
            String value = troops.nextToken();

            else if (list.equals("Done")) {
                troop trooper=new troop();
                trooper.troopLoaded(kingdom,lord,troop,troopAmount,weapon,animal);

}

/////

troop class:

   public void troopLoaded(ArrayList<String> kingdom,ArrayList<String> lord,ArrayList<String> troop,ArrayList<String>,troopAmount,ArrayList<String> weapon,ArrayList<String> animal) {

    System.out.println(kingdom);
    System.out.println(lord);
    System.out.println(troop);
    System.out.println(troopAmount);
    System.out.println(weapon);
    System.out.println(animal);

}

how do I give troop class the arraylists without over writing them in the troop class?

1
  • no I can't, there is a stat called weapon, the troop can have as many weapons as he wants, ie sword, axe, polaxe... Commented Jan 14, 2012 at 3:31

4 Answers 4

2
import java.util.ArrayList;
import java.util.List;

class Item {
    private name;

    public String getName() {
        return name;
    }

    private void setName(String name) {
        this.name = name;
    }


public class ItemManager {

    public static List<ArrayList<Item>> megaList;
    public static void main(String args) {

        megaList = new ArrayList<ArrayList<Item>>();

        ArrayList<Item> someItems = new ArrayList<Item>();
        someItems.add(new Item());
        someItems.add(new Item());

        megaList.add(someItems);

        for(ArrayList<Item> list : megaList ) {
            for (Item item : list) {
                String nameOfItem = item.getName();
            }
        }
    }

Then you can iterate inside the megaList and recursively iterate inside the someItems.

Sign up to request clarification or add additional context in comments.

1 Comment

I added the iteration part and sample Item methods.
1

If I understand your question correctly you can use:

ArrayList<ArrayList<Object>>

1 Comment

cengiz said the exact samething but in more detail, giving him the check, sorry. I up voted you though.
0

IIUC, what you want is not passing all those parameters to the troopLoaded() method?

You may consider using a Map of ArrayLists:

Map<String,ArrayList<String>> map = new HashMap<String,ArrayList<String>>();

map.put("kingdom", new ArrayList<String());
...

map.put("animal", new ArrayList<String());

then calling:

trooper.troopLoaded(map);

1 Comment

I dont think you understand my question. The problem is that each stat is also unknown (along with the other variables), so, there could be 10 troops who have from 0-100 different weapons, and later in the program, i'll have to somehow link every troop to there weapons but don't know how.
0

I just used a 2d array array[][]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.