简体   繁体   中英

Java array filled with what is in the last slot

So i'm using this for loop to fill a small array with 5 random things from a larger array but i have a problem where when i fill the original array with this new object it fills every slot above it with the object that was just put in. so everything in my smaller array is the same as well.

`public Prize[] Showcase(){

        Scanner fileIn=null;
        try{
            fileIn = new Scanner(new File("prizeList.txt"));    
            for(int i=0;fileIn.hasNextLine();i++){
                String delim ="\t";
                String[] splitStrings=fileIn.nextLine().split(delim);
                if(splitStrings.length==2){
                    int thing = Integer.parseInt(splitStrings[1]);
                    prizeList[i]=new Prize(splitStrings[0],thing);


                }

            }
        }
        catch(Exception e){
            System.out.println(e);              
        }
        return prizeList;

    }
    public static void Prizes(Prize[] prizeList){
        for(int i=0;i<5;i++){
        Random bkRandy=new Random();
        int randy = bkRandy.nextInt(50);
        prizes[i]=prizeList[i];
        System.out.println(prizes[i].getName());
        }
    }`

i'm not sure what's going on i've never seen it do anything like this. I dont get any errors or anything there is just something wrong with the loop i suspect.

My question is "why is it filling every slot with the last object?"

public class Prize {
    private static String name;
    private static int price;
    public Prize( String name, int price){
        this.name = name;
        this.price = price;
    }
    public static int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    public static String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }   
}

When you make something static there is only one. Your instances are therefore unique references that share the same data. You need something like

public class Prize {
    private String name;  // <-- not static.
    private int price;  // <-- not static.
    public Prize(String name, int price){
        this.name = name;
        this.price = price;
    }
    public int getPrice() {  // <-- not static.
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    public String getName() { // <-- not static.
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }   
}

With your Prize code, you would only get the "last" value because every instance shared the class level fields ( static ).

If I understand your question correctly, you are asking how to get a list of random items from another list. I'm assuming you specifically don't want duplicates (ie the short list should not contain more than one item from the longer list). If that's correct then I would suggest the following:

List<Prize> getRandomPrizes(List<Prize> prizes, int prizeCount) {
    Collections.shuffle(prizes);
    return prizes.stream().limit(prizeCount).collect(Collectors.toList());
}

That automatically copes with the situation in which there are less than prizeCount prizes in the original list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM