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?
Asked
Active
Viewed 4.9k times
4

Flexo
- 87,323
- 22
- 191
- 272

Sanat Pandey
- 4,081
- 17
- 75
- 132
7 Answers
11
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();
}
}

Jayendra
- 52,349
- 4
- 80
- 90
6
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());
}
}

Farm
- 3,356
- 2
- 31
- 32
-
Correct me if I am wrong but this does not sound right in all cases, because sometimes the user of this api may want to get the list using `getList()` and add some elements to it. What do you think? – Kuldeep Jain Jul 28 '16 at 14:37
3
ArrayList<String> arrList = new ArrayList<String>();
public ArrayList<String> getArrList() {
return arrList;
}
public void setArrList(ArrayList<String> arrList) {
this.arrList = arrList;
}

Lalit Poptani
- 67,150
- 23
- 161
- 242
1
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;
}

Dharmendra
- 33,296
- 22
- 86
- 129
0
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

swiftBoy
- 35,607
- 26
- 136
- 135
0
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;
}

Fan Wu
- 237
- 1
- 4
- 11
-
I haven't see this kind of declaration....private List arrayList = new arrayList; – Lalit Poptani Sep 26 '11 at 06:28
-
-1
Try these
public ArrayList getArrayList() {
return arraylist;
}
public void setArrayList(ArrayList arraylist) {
this.arraylist = arraylist;
}

Rahul Choudhary
- 3,789
- 2
- 30
- 30
-
1
-
@LalitPoptani Buddy it is understood that he has a member variable. Why would you be writing a getter/setter for otherwise. – Rahul Choudhary Sep 26 '11 at 06:43
-
@RahulChoudhary OMG, public void setArrayList(ArrayList arraylist) { this.arraylist = arraylist; } is this the way you set and arrayList? instead of changing your answer you are bouncing back on me. – Lalit Poptani Sep 26 '11 at 06:48