I am new to arraylist concept. Please help me how to declare arraylist and add strings and integers to it.
String str = quesid+","+k1;
ArrayList<Object> arl=new ArrayList<Object>();
arl.add("str");
In the above code the string didnt added to arraylist. Where I went wrong...plz help me
Thanks in advance...
Make a custom class
, and put String and Int member fields now also make a getter() setter() methods for that fields in that class, Now use that class in your ArrayList, Simple :-)
EDIT: Example
List<CustomClass> foo = ArrayList<CustomClass>
public class CustomClass
{
private String result;
private int count;
public CustomClass() {
super();
// TODO Auto-generated constructor stub
}
public void setResult(String res)
{
this.result=res;
}
public void setCount(int cnt)
{
this.count=cnt;
}
public String getResult()
{
return this.result;
}
public int getCount()
{
return this.count;
}
}
In your activity,
List<CustomClass> foo = ArrayList<CustomClass>
CustomClass cust = new CustomClass();
cust.setResult("Test");
cust.setCount(1);
foo.add(cust);
To retrieve data...
String res = foo.get(0).getResult();
int count = foo.get(0).getCount();
You can directly set a values by passing it to CustomClass constructor .. (In this case you have to modify constructor of CustomClass) Choice is yours.
Define a class as such:
public class StringInt{
private String s;
private int i;
public StringInt(String o1, int o2){
s = o1;
i = o2;
}
public String getString(){
return s;
}
public int getInt(){
return i;
}
public void setString(String s){
this.s = s;
}
public void setInt(int i){
this.i = i;
}
}
Then use is in an ArrayList
ArrayList<StringInt> arl = new ArrayList<StringInt>();
arl.add(new StringInt("Test", 15));
Edit: user370305 had the same idea. Great minds think alike? :P
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.