user1621988
user1621988

Reputation: 4335

Object of ArrayLists, how to add/remove them from the object?

public class MapData {

    ArrayList<String> Redsp = new ArrayList<String>();
    ArrayList<String> Bluesp = new ArrayList<String>();
    ArrayList<String> Playersp = new ArrayList<String>();

    public MapData(ArrayList<String> redsp, ArrayList<String> bluesp, ArrayList<String> playersp) {
        Redsp = redsp;
        Bluesp = bluesp;
        Playersp = playersp;
    } 
}

How do I make a object of MapData, and add/remove items to/from the object? I would like to add like 6 items to bluesp and redsp, and 20 to playersp.

MapData TEST = new MapData(null,null,null);
TEST.??

Upvotes: 0

Views: 1361

Answers (4)

Baz
Baz

Reputation: 36894

You can either add getter and setter methods for the Lists to MapData and add new elements like this:

TEST.getRedsp().add("hello");

with

public ArrayList<String> getRedsp()
{
    if(Redsp == null)
        Redsp = new ArrayList<String>();

    return Redsp;
}

or you can introduce an add method for each list to MapData:

TEST.addToRedsp("hello");

with

public boolean addToRedsp(String value)
{
    if(Redsp == null)
        Redsp = new ArrayList<String>();

    return Redsp.add(value)
}

Proceed similarly for the delete case.

By the way: Have a look at variable naming conventions.

Upvotes: 0

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

Use add() and remove() methods

- You can opt for creating the adding and removing methods Or make all the ArrayList as static

Eg:

public void addToRedSp(String string) {

    Redsp.add(string);

} 


public void remToRedSp(String string) {

    Redsp.remove(string);

} 

MapData TEST = new MapData(null,null,null);

    // To add

    test.addToRedSp("Vivek");

    // To remove

    test.remToRedSp(0);   // or  MapData.Redsp.remove("Vivek");

- ArrayList as static

public static ArrayList<String> Redsp = new ArrayList<String>();
public static ArrayList<String> Bluesp = new ArrayList<String>();
public static ArrayList<String> Playersp = new ArrayList<String>();



MapData TEST = new MapData(null,null,null);

// To add

MapData.Redsp.add("Vivek");

// To remove

MapData.Redsp.remove(0);   // or  MapData.Redsp.remove("Vivek");

Upvotes: 0

Boris Strandjev
Boris Strandjev

Reputation: 46943

The easiest way is to provide getters for the three collections and then manipulate them the ordinary way. Such manipulations will take effect on the member fields:

public class MapData {

    ArrayList<String> Redsp = new ArrayList<String>();
    ArrayList<String> Bluesp = new ArrayList<String>();
    ArrayList<String> Playersp = new ArrayList<String>();

    public MapData(ArrayList<String> redsp, ArrayList<String> bluesp, ArrayList<String> playersp) {
        Redsp = redsp;
        Bluesp = bluesp;
        Playersp = playersp;
    }

    public ArrayList<String> getRedsp(); 
}

And then you do:

MapData TEST = new MapData(null,null,null);
TEST.getRedsp().add("Text1");
TEST.getRedsp().add("Text2");

and so on.

However, take care: you construct not with empty lists, but with null and my code will trigger NPE. Consider setting the default values to empty lists.

Upvotes: 0

RNJ
RNJ

Reputation: 15552

I would create some more methods to MapData

For example to add to Bluesp

public void addToBlueSp(String string) {
    Bluesp.add(string);
} 

Also I would use camelCase as this is the standard thing to do in Java.

I would probably recommend creating the ArrayLists inside the ctor too as there is little point passing them into an object and then using that object to add/remove items from them. If you have the ArrayList you could add them outside of this object. But that is a design thing...

Upvotes: 2

Related Questions