Adding ArrayList to ArrayListArrayList - android

Suppose we have an ArrayListArrayList(String)
[[a,b,c,d] , [e,f,g,h]]
and an ArrayList(String)
[1,2,3]
How can we add ArrayList to ArrayListArrayList in position 1 at the end in order to get
[[a,b,c,d] , [e,f,g,h,1,2,3]]
Thanks

//pseudocode
List<ArrayList<String>> arraylistarraylist = [[a, b, c, d], [e, f, g, h]];
List<String> list = Arrays.asList(1,2,3);
arraylistarraylist.get(1).addAll(list);

If we name your ArrayList<ArrayList> m and if we name your ArrayList a, then you could use
m.get(1).addAll(a)
Description for this method provided by Oracle here: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

I think you need a add(int, E) method
List<ArrayList<String>> dataHolder = new ArrayList<ArrayList<String>>();
List<String> firstData = new ArrayList<String>();
firstData.add("a");
firstData.add("b");
firstData.add("c");
firstData.add("d");
List<String> secondData = new ArrayList<String>();
secondData.add("e");
secondData.add("f");
secondData.add("g");
secondData.add("h");
secondData.add("1");
secondData.add("2");
secondData.add("3");
dataHolder.add(secondData);
dataHolder.add(0, firstData); // insert your List<String> to 0 index position in dataHolder

Related

How to display the number of items present is a list using loop

I have a list having number of questions in it.
I want to display the total no. of question present in the list.
Loop is used to get particular element at particular index like :
List<?> list = new ArrayList<>();
for(Object obj : list){
//---- do something with obj
}
You do not need loop for size, there are methods to get size of collected data.
Well it not clear which list you are referring to, but here is some example of how you can get size/length of collected data..
if you are referring to ListView,use getCount() :
// --------------ListView
ListView listView = new ListView(context);
int listViewSize = listView.getCount();
And if your referring to any Collection , use size() :
// --------------Collection
ArrayList<?> arrayList = new ArrayList<>();
int arrayListSize = arrayList.size();
List<?> list = new ArrayList<>();
int listSize = list.size();
Set<?> stringSet = new HashSet<>();
int setSize = stringSet.size();
Map<?,?> hashMap = new HashMap<>();
int mapSize = hashMap.size();
LinkedList<?> link = new LinkedList<>();
int linkedListSize = link.size();
Queue<?> queue = new LinkedList<>();
int queueSize = queue.size();
<?> indicate generic, use data-type (<String>,<Integer> etc) for particular return type..
Any other data type array, use length :
// ---------------Data Type
String[] stringsArray = new String[]{};
int stringArraySize = stringsArray.length;
int[] in = new int[]{};
int intSize = in.length;
This is java document for explanation
I believe you are looking for the below. Although the question is not clear.
for(int i = 0;i<list.size();i++){
System.out.println(list.get(i));
}
use size() method to get the size of the list

removing item from copied list also removes from original

public static void printList(ArrayList<Col> list, String place)
{
ArrayList<Col> list2 = list;
int count=0;
float r,g,b;
while(!list2.isEmpty())
{
r = list2.get(count).getR();
g = list2.get(count).getG();
b = list2.get(count).getB();
list2.remove(0);
Log.i(place+": "+count + "", "R: "+r+" G: "+g+" B: "+b);
}
}
this method is removing all the items from my original list for some reason..
i'm thinking maybe the way i duplicate the list is wrong, but i couldnt find the right way.
Since ArrayList is an object:
ArrayList<Col> list2 = list;
is a soft copy. It only copies a reference to the same ArrayList object.
In Java you can clone an object like so:
ArrayList<Col> list2 = (ArrayList<Col>)list.clone();
USE
ArrayList list2 = new ArrayList(list);
INSTEAD OF
ArrayList list2 = list;
Use the following code before assigning the value:
ArrayList<Col> list2 = new ArrayList<Col> ();

Cannot cast from String[] to ArrayList<String>

I have a resource string array that I want to put into an ArrayList, however the datatype that comes back is a String array. The string[] cannot be directly cast to ArrayList w/o receiving the following error:
Cannot cast from String[] to ArrayList
How to I convert the String[] datatype to ArrayList?
Edit
I have an adapter whose constructor takes an ArrayList and a resource string-array that I want to populate it.
If you can make do with a List<String> (and not specifically an ArrayList<String>), you can use:
List<String> list = Arrays.asList(stringArray);
Otherwise, you can do this:
ArrayList<String> list = new ArrayList<String>(Arrays.asList(stringArray));
However, the latter is less efficient (in both time and object creation count) than the other suggested solutions. Its only benefit is that it keeps the code down to one line and is easy to comprehend at a glance.
public ArrayList<String> arrayToArrayList(String[] array){
ArrayList<String> arrayList = new ArrayList<String>(array.length);
for(String string : array){
arrayList.add(string);
}
return arrayList;
}
how about this
import java.util.Collections;
String[] myStringArray = new String[] {"foo", "bar", "baz"};
List myList = new ArrayList(myStringArray.length);
Collections.addAll(myList, myStringArray);

How to add a value to arraylist in android?

I am adding a value to my arraylist but it is showing error. Before adding the value I am displaying it first. The value is displaying but it is not adding to the arraylist. Please help me regarding this...
My Code:
static ArrayList<String> allfirstids;
ArrayList<String> list = List.get(i);
UserBO user = new UserBO();
user.firstid = Integer.parseInt(list.get(0));
user.secondid = Integer.parseInt(list.get(1));
System.out.print("Hello this is first id");
System.out.print(list.get(0));
allfirstids.add(list.get(0));
System.out.println("first ids"+allfirstids);
Thanks in advance...
before adding, please initialize arraylist.
allfirstids = new ArrayList<String>();
You forgot to initialize your static ArrayList allfirstids, Just initialize it, before using..
static ArrayList<String> allfirstids = new ArrayList<String>();
ArrayList<String> list = List.get(i);
UserBO user = new UserBO();
user.firstid = Integer.parseInt(list.get(0));
user.secondid = Integer.parseInt(list.get(1));
System.out.print("Hello this is first id");
System.out.print(list.get(0));
allfirstids.add(list.get(0));
System.out.println("first ids"+allfirstids);

How to add one arraylist to other arraylist in android?

I have created 3 arraylists and stored data in it. I need to pass one single arraydata to other page and so I have added my individual arrays to the main array. But when I added one array to other the data is not being aded to that array. Here is my code:
static ArrayList<ArrayList<String>> stringList1=new ArrayList<ArrayList<String>>();
static ArrayList<ArrayList<String>> stringList3;
stringList1 = Mypage.stringList1;
ArrayList<String> optionlist = new ArrayList<String>();
stringList3 = new ArrayList<ArrayList<String>>();stringList3 = dbAdapter.selectRecordsFromDBList(query2, null);
for (int i = 0; i < stringList3.size(); i++) {
optionlist = stringList3.get(i);
System.out.print("option list size");
System.out.print(optionlist.size());
stringList1.add(optionlist);
System.out.println("total stringlist1"+stringList1.get(0));
I am getting the stringList1 array values from Mypage and accessing that in new page. In the new page I am trying to add the optionlist array to stringList1 by giving stringList1.add(optionlist) but the data is not adding. Where I went wrong? Please help me regarding this... Thanks in Advance
Use addAll() It appends all of the elements in the specified ArrayList to the end of this list, in the order that they are returned by the specified Collection's Iterator.
use stringList1.addAll(optionlist); instead of stringList1.add(optionlist);
After seeing your code I can guess that you have problem that elements of arrayList is not copy in another arrayList.If this is your issue then change your code like below
static ArrayList<ArrayList<String>> stringList1=new ArrayList<ArrayList<String>>();
static ArrayList<ArrayList<String>> stringList3;
stringList1 = Mypage.stringList1;
ArrayList<String> optionlist;
stringList3 = new ArrayList<ArrayList<String>>();
stringList3 = dbAdapter.selectRecordsFromDBList(query2, null);
for (int i = 0; i < stringList3.size(); i++) {
optionlist = new ArrayList<String>();
optionlist = stringList3.get(i);
System.out.print("option list size");
System.out.print(optionlist.size());
stringList1.add(optionlist);
System.out.println("total stringlist1"+stringList1.get(0));
}
The problem seems that you are initializing your
ArrayList<String> optionlist;
only once that is outside the loop. So, in that case only the first ArrayList gets stored. So, initialized the ArrayList<String> optionlist inside the loop.
ArrayList<String> optionlist;
for (int i = 0; i < stringList3.size(); i++) {
optionlist = new ArrayList<String>();
optionlist = stringList3.get(i);
stringList1.add(optionlist);
}

Categories

Resources