Sanat Pandey
Sanat Pandey

Reputation: 4103

Using Getter and Setter for arraylist in java

I have a problem that I want to set and get an ArrayList from setter and getter methods of android. But I am new to android and Java and don't know how to do that? Can anyone help me regarding this problem?

Upvotes: 4

Views: 49088

Answers (7)

user6497465
user6497465

Reputation: 1

please try this

  public static ArrayList<books> al = new ArrayList<books>();
  books book=new books(id,name,type);
                book1 b1=new book1(id,name,type);
                al.add(book);
                al.add(book1);   //error at book1

Upvotes: 0

Farm
Farm

Reputation: 3396

For better Encapsulation / OO design I would do following

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TestListGetterSetter {
List<String> list = new ArrayList<>();

//Return copy of the list
public List<String> getList() {
    return new ArrayList<>(list);
}

// Copy source list into destination list
public void setList(List<String> listToBeSet) {
    if (listToBeSet != null)
        this.list.addAll(listToBeSet);
}

public static void main(String[] args) {
    TestListGetterSetter testListGetterSetter = new TestListGetterSetter();

    List<String> clientList = new ArrayList<>(Arrays.asList("foo", "bar", "HiHa"));

    testListGetterSetter.setList(clientList);
    System.out.println("TestListGetterSetter.list before clientList modification = " + testListGetterSetter.getList());

    //Now you can change "clientList" without affecting testListGetterSetter object
    clientList.add("1");
    System.out.println("clientList modified List = " + clientList);
    System.out.println("TestListGetterSetter.list after clientList modification = " + testListGetterSetter.getList());
  }
}

Upvotes: 6

Dharmendra
Dharmendra

Reputation: 33996

Generally getter and setter methods are for assign variable and get variables value from that

There is not any difference of getter and setter methods for arraylist or for int of a class 

ArrayList<String> arrList;

    public ArrayList<String> getArrList() {
        return arrList;
    }

    public void setArrList(ArrayList<String> arrList) {
        this.arrList = arrList;
    }

Same for Int

int id;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

Upvotes: 1

Fan Wu
Fan Wu

Reputation: 237

I am not familiar with android. But in JAVA, if you want to use set and get method, you need to declare the List first.

private List arrayList = new ArrayList();

public List getArrayList() {
    return arrayList;
}

public void setArrayList(List arrayList) {
    this.arrayList = arrayList;
}

Upvotes: 0

Rahul Choudhary
Rahul Choudhary

Reputation: 3809

Try these

public ArrayList getArrayList() {
    return arraylist;
}

public void setArrayList(ArrayList arraylist) {
    this.arraylist = arraylist;
}

Upvotes: -1

Lalit Poptani
Lalit Poptani

Reputation: 67286

    ArrayList<String> arrList = new ArrayList<String>();  

    public ArrayList<String> getArrList() {
        return arrList;
    }

    public void setArrList(ArrayList<String> arrList) {
        this.arrList = arrList;
    }

Upvotes: 3

Jayendra
Jayendra

Reputation: 52769

Example -

import java.util.ArrayList;
import java.util.List;

public class Test {

List<String> list = null;

public List<String> getList() {
    return list;
}

public void setList(List<String> list) {
    this.list = list;
}

public static void main(String[] args) {
    Test test = new Test();
    List<String> sample = new ArrayList<String>();
    sample.add("element 1");
    test.setList(sample);
    List<String> sample1 = test.getList();
}
}

Upvotes: 11

Related Questions