Can I change one value for a Object while comparing it to another array?

Liam :

I have an object which has a name, String [] there is also a another String array which just stores the name of the object (The first parameter) which is in main.

import java.util.*;
class Dice{
    public String [] side;
    public String name;

    public Dice (String n, String ... a){
        name = n;
        side = a;

    }

    //Setter and Getter name
    public String getName(){
        return name;
    }
    public void setName(String n){
        name = n;
    }
}

The objects parameters are set in the main class.

Dice easy = new Dice("Green:","Brain","Brain","Brain","Foot Print","Foot Print","Shotgun");

The string array just stores the name Easy:.

I am trying to compare the two arrays by passing them into a method in main.

//Removeing the 3 dice which were picked form the cup of the current player
public static Dice [] cup(Dice [] a , String [] b){
    Dice [] currentCup = new Dice[a.length];


    for (int i = 0; i < b.length; i++) {
        if (b[i] == a[i].getName()) {
            currentCup[i].setName("");
        }
    }

    return currentCup;
}

If the name of the object equals the name in the String array the objects name should equal and empty String(" ").

I am getting a error

Exception in thread "main" java.lang.NullPointerException

I understand that an ArrayList is much better to use here as I can just .remove(i, elem). But I do not know how to pass an ArrayList into a constructor.

Also this is just pure practice for myself using arrays.

The result should be that if the Dice [].getName() equals easy the name of that Dice object should be an empty String " ".

Jude Niroshan :

I understand that an ArrayList is much better to use here as I can just .remove(i, elem). But I do not know how to pass an ArrayList into a constructor.

public Dice (String n, List<String> sideList){
    name = n;
    side = sideList.toArray(new String[sideList.size()]);

}

Also you are not doing anything with the below array.

Dice [] currentCup = new Dice[a.length];

I would highly encourage you to dry run the code by yourself. It seems that your code is not doing what you really what to do. :)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=164483&siteId=1