Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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;
}
share|improve this question
    
So you need to get back each element in the array as object of its initial type, right? –  Ahmed Hamdy 21 hours ago
add comment

2 Answers

up vote 1 down vote accepted

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.
share|improve this answer
    
I looked at those and found them doing their work correctly but I will post an EDIT: shortly –  Vitalij Kornijenko 21 hours ago
    
@VitalijKornijenko While at it, make your code a minimal example that we can run, and add what output you get and what you expect. –  user1803551 21 hours ago
    
@jihinpt you can put your comment in comment area cuz it is not an answer. you should delete it in order avoiding to get voted down. –  Kick Buttowski 21 hours ago
1  
@Kick: Sure. I'll edit it. Its just that it doesnt allow me to add comments since my points is less than 50. Sorry about that. –  jithinpt 21 hours ago
1  
@jithinpt your formatting is too bad. just fix it up cuz you gonna get voted down too –  Kick Buttowski 21 hours ago
show 3 more comments

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;
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.