Vitalij Kornijenko
Vitalij Kornijenko

Reputation: 559

Creating new objects from array to array

I'm trying to generate new unique objects from an array of all possible objects to another array. The idea is that I have 3 classes that implement Region class and they have their own methods. These 3 classes are in my ArrayList<Region> arr. I pick a random class and add it to ArrayList<Region> ALL_REGIONS in a for loop. The problem is that the object that is added from arr is not unique, they are the same. This ca be told by their name. Every Region must have it's unique name and other settings but they don't. So this is the code I have so far:

public void generateRegions(){
    ArrayList<Region> arr = Regions.getAllRegions();
    Random rnd = new Random();
    String ntype;
    int regcounter = 5;
    int b;

    for(int i = 0; i < regcounter; i++){
        ALL_REGIONS.add(arr.get(rnd.nextInt(arr.size())));
        ntype = "n" + ALL_REGIONS.get(i).getType();

        b = rnd.nextInt(Regions.getNtypeSize(ntype));
        UI.print("b: " + b);
        ALL_REGIONS.get(i).setName(Regions.getArrayName(ntype, b));
    }
}

public static ArrayList<Region> getAllRegions(){
    ArrayList<Region> arr = new ArrayList<Region>();

    arr.add(new Highlands());
    arr.add(new Cave());
    arr.add(new Oasis());

    return arr;
}

getArrayName returns a String name of the Region from an array and getNtypeSize returns an int, size of the array String[] that contatins all names which is not really important just now.

So.. how can I have every Cave, every Oasis unique/as a separate object?

**EDIT: ** Requested getArrayName() and getNtypeSize() methods are below:

public static String getArrayName(String ntype, int t) {
    String ans = null;

    if(ntype.equals("ncave")){
        if(t<=ncaveSize)
            ans = ncave[t];
    }else if(ntype.equals("noasis")){
        if(t<=noasisSize)
            ans = noasis[t];
    }else if(ntype.equals("nhighlands")){
        if(t<=noasisSize)
            ans = nhighlands[t];
    }

    //Can happen when t is bigger then ntype size or
    // if ntype string is wrong
    if(ans == null){
        UI.printerr("getArrayNames: ans is empty/null");
    }
    UI.printerr(ans);
    return ans;
}

public static int getNtypeSize(String ntype){
    int ans = 0;

    if(ntype.equals("ncave")){
            ans = ncaveSize;
    }else if(ntype.equals("noasis")){
            ans = noasisSize;
    }else if(ntype.equals("nhighlands")){
            ans = nhighlandsSize;
    }else
        UI.printerr("getNtypeSize: returned 0 as an error");

    return ans;
}

Upvotes: 0

Views: 81

Answers (2)

jithinpt
jithinpt

Reputation: 1244

The issue is in this line:

ALL_REGIONS.add(arr.get(rnd.nextInt(arr.size())));

Here, you're not adding a new object to ALL_REGIONS. Rather, each time you're adding a reference to an object in 'arr'.

For example, each time rnd.nextInt(arr.size()) returns 2, you would add a reference to arr[2] to ALL_REGIONS. Thus, effectively, each entry in ALL_REGIONS refers to one of the objects in arr. (In this specific example, one of 3 objects you added in getAllRegions())

Effectively, this means that every Highlands object reference in ALL_REGIONS points to the same object => arr[0] Similarly, every Cave reference in ALL_REGIONS points to arr[1] and every Oasis reference points to arr[2]

Something along this line should fix the issue:

Region reg = arr.get(rnd.nextInt(arr.size()))  
ALL_REGIONS.add(reg.clone()); // this is just meant to be a sort of pseudo-code. Use a clone() method to create a new copy of the object and that copy to ALL_REGIONS.

Upvotes: 1

Ahmed Hamdy
Ahmed Hamdy

Reputation: 2897

If I got it right? You want to cast back to the type of the original object. It is plenty easy to do so, you will use some of the Java Polymorphism concepts. You will use a function called InstanceOf like this

Region ob = arr[0];
if (ob instanceof Highlands)
    Highlands newOb = (Highlands) ob;

Upvotes: 1

Related Questions