insert array into an array - java

In my android application i need to insert an array into an array and access its values.
Is there any way that i can get this done.
Please share your valuable suggestions
Thanks in advance :)

So what's the issue?
Give this a try:
ArrayList<ClassName> l1 = new ArrayList<ClassName> ();
Now suppose you have array objects arr1 and arr2. You can add them to an ArrayList with the following:
l1.add(arr1);
l1.add(arr2);
Now you can access each element in l1 with
for(int i=0; i < l1.size;i++){
<ClassName> obj = l1.get(i);
// and do what you want to do
}

You are basically making a list of lists. Create a new list then add that list to the master list. Use generics so you don't have to do a lot of casting.
List<Object> listOfObjects = new ArrayList<Object>();
listOfObjects.add(obj1);
listOfObjects.add(obj2);
List<List<Object>> listOfLists = new ArrayList<List<Object>>();
listOfLists.add(listOfObjects);
// get first object in first list
listOfLists.get(0).get(0);
// add to the first list
listOfLists.get(0).add(0);

Here a example, but for It is for C#
ArrayList MainArray = new ArrayList();
MainArray.Add(new ArrayList());
MainArray.Add(new ArrayList());
MainArray.Add(new ArrayList());
(MainArray[1] as ArrayList).Add("Hello");
Response.Write((MainArray[1] as ArrayList)[0].ToString());

String myArray[] = new int[10];
for(int i=0; i<myArray.length(); i++) {
myArray[i] =value adding here;
}

Related

Storing an ArrayList in an 2d Array Java

How can I store an ArrayList in a two dimensional array?
I've tried it like this, but it won't work:
ArrayList<Integer> arrList = new ArrayList<Integer>();
ArrayList<Integer>[][] arr = new ArrayList<Integer>[9][9];
but it won't even let me declare the ArrayList-Array.
Is there a way to store a list in a 2d array?
Thanks in advance!
You can't create arrays of generic types in Java. But this compiles:
ArrayList<Integer> arrList = new ArrayList<Integer>();
ArrayList<Integer>[][] arr = (ArrayList<Integer>[][]) new ArrayList[9][9];
arr[0][0] = arrList;
Why can't you create these arrays? According to the Generics FAQ, because of this problem:
Pair<Integer,Integer>[] intPairArr = new Pair<Integer,Integer>[10]; // illegal
Object[] objArr = intPairArr;
objArr[0] = new Pair<String,String>("",""); // should fail, but would succeed
Assuming you want an ArrayList inside an ArrayList inside yet another ArrayList, you can simply specify that in your type declaration:
ArrayList<ArrayList<ArrayList<Integer>>> foo = new ArrayList<ArrayList<ArrayList<Integer>>>();
Entries can be accessed via:
Integer myInt = foo.get(1).get(2).get(3);
Just be wary of boundaries - if you try to access an out of bounds index you'll see Exceptions thrown.

java - "int cannot be converted to ArrayList"

I'm trying to create a two dimensional ArrayList which will store ints, strings & booleans.
I've got as far as entering the first int, but I get a red squiggle and the error "int cannot be converted to ArrayList".
ArrayList[][] qarray= new ArrayList [10][5];
qarray[0][0]= 1;
BTW, googling the phrase "int cannot be converted to ArrayList" is giving me exactly six results.
The error is correct.
Your array type is ArrayList. You can insert only ArrayLists in that array.
If you want to store int's, your declaration should be.
int[][] qarray= new int [10][5];
And also, as someone commented, you cannot store strings and booleans in this array anymore.
ArrayList[][] qarray= new ArrayList [10][5];
Basically your code creates 2 dimensional array list objects (50 array list objects).
qarray[0][0]= 1;
And you are trying to assign integer, where you need to create a ArrayList object. It expects something like
qarray[0][0]= new ArrayList();
However this would not meet your objective. The following piece of code could meet your objectives:
ArrayList[] qarray = new ArrayList[10];
qarray[0]= new ArrayList();
qarray[0].add(1);
qarray[1]= new ArrayList();
qarray[1].add(true);
qarray[2]= new ArrayList();
qarray[2].add("hello");
Try like this:
List<Integer> qarray = new ArrayList<>();
qarray.add(1);
First I believe that you need array and not ArrayList. 2d array can be created as following.
int[][] arr = new int [10][10];
Your next problem is that you tried to assign int constant 1 to variable of other type. The following example shows how to assign int to element of array
arr [0][0] = 1;
According to javadoc, you can not create arrays of ArrayList. Use 2D array instead.
If you need 2D ArrayList anyway, you should have tried this way:
ArrayList<ArrayList<Integer>> listOfLists = new ArrayList<>();
ArrayList<Integer> list1 = new ArrayList<>();
list1.add(1);
list1.add(2);
ArrayList<Integer> list2 = new ArrayList<>();
list2.add(5);
listOfLists.add(list1);
listOfLists.add(list2);
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
you wan to add int,String and boolean then you can use
ArrayList<ArrayList<Object>> listOfLists = new ArrayList<ArrayList<Object>>();
it will help you
ArrayList<Integer>[][] list = new ArrayList[10][10];
list[0][0] = new ArrayList<>();
list[0][0].add(new Integer(10);
try like this.

How to maintain the value of old arraylist after copied to a new arraylist and manipulation

I have an arraylist as below:
ArrayList<List<Double>> myAr = new ArrayList<>();
I add the value to the array as below:
myAr.add(Arrays.asList((double)3,(double)4,(double)5,(double)6));
myAr.add(Arrays.asList((double)6, (double)8, (double)1, (double)4));
Then I assign to a new arraylist and copied myAr to temp as below:
ArrayList<List<Double>> temp = new ArrayList<>();
temp = (ArrayList<List<Double>>)myAr.clone();
I did some modification to an element as below:
temp.get(0).set(2, 9.0);
Then I display the output for both temp and myAr.
System.out.println(temp);
System.out.println(myAr);
Unfortunately, both displayed the same output. I want the myAr to be remain as it is. What can I do?
Thank you
You need to implement a deep copy, i.e. you actually need to duplicate the inner lists:
ArrayList<List<Double>> temp = new ArrayList<>();
for (List<Double> list : myAr) {
temp.add(Arrays.asList(list.toArray(new Double[0]));
}
otherwise the elements of temp and myAr point to the same underlying lists.
This is happening because the ArrayList you are clonig ie myAr contains(not primitive types) .To achieve this task you have to individually copy all the elements using a for loop.
Use the following code, it will work:
ArrayList<List<Double>> myAr = new ArrayList<>();
myAr.add(Arrays.asList((double)3,(double)4,(double)5,(double)6));
myAr.add(Arrays.asList((double)6, (double)8, (double)1, (double)4));
ArrayList<List<Double>> temp = new ArrayList<>();
//copy old list to new list (deep copy)
for (List<Double> list : myAr)
{
temp.add(list);
}
//output original list
for (List<Double> list : myAr)
{
System.out.println(list);
}
System.out.println();
System.out.println("Temp array");
//editing value in new list
temp.get(0).set(2, 11.0);
//output new list
for (List<Double> list : temp)
{
System.out.println(list);
}
.clone() creates a shallow copy (i.e. it creates a new collection and copies the pointers of the values in the collection, but does not copy the values).
If you want to achieve a deep copy, use a copy constructor:
temp = new ArrayList<>(myArr);

Copying arraylist

I think doing arraylist1=arraylist2 makes the 2 of them share the same memory. How can I copy an arraylist without them doing that? I want to treat them sepparately.
List<Integer> rez = new ArrayList<>();
List<Integer> rezc = new ArrayList<>();
rez.add(1);
rezc=rez;
rezc.add(2);
for (int s : rez) {
System.out.print(s + " ");
}//this will print 1 2
I think doing arraylist1=arraylist2 makes the 2 of them share the same memory.
Not quite, it makes both of those references refer to the same, single, object.
How can I copy an arraylist without them doing that?
Lots of options:
ArrayList has a copy constructor:
List<Integer> rezc = new ArrayList<>(rez);
List has an addAll method:
List<Integer> rezc = new ArrayList<>();
rezc.addAll(rez);
ArrayList has a clone method, but it's a bit ugly to use if rez is declared as a List because you have to assume it's an ArrayList and cast it, which is probably not a great idea:
List<Integer> rezc = (List<Integer>)((ArrayList<Integer>)rez).clone();
It's well worth reading through the JavaDoc when trying to figure things like this out.
The statement arraylist1=arraylist2 means they are referring to same ArrayList object. Reference variables arraylist1 and arraylist2 are referring to same object and hence, the changes done by arraylist1 will be seen when you are trying to access the object by arraylist2
If you want to make a new ArrayList then, ArrayList rezc = new ArrayList(rez)
Instead of this line rezc=rez;
Use List<Integer> rezc = new ArrayList<>(rez);
The long hand way is a for loop to cycle though one list while adding all the items to the second. Something like
for (int I = 0; I < rez.size() I++) {
rezc.add(rez.get(I)); }
But the previous answers are much more efficient.
If I understand correctly you are talking about Java shallow cloning v/s deep cloning. In this case the below code might help
List<Integer> rez = new ArrayList<>();
List<Integer> rezc = new ArrayList<>();
rez.add(1);
rezc.addAll(rez); // addAll
List<Integer> rezc2 = (List<Integer>)((ArrayList<Integer>)rez).clone(); //clone

Java performance avoid creating new in loop

I have a code like this
Map<String, List> searchMap = new HashMap<String, List>();
for(int i=0; i<something.size(); i++){
List<String> list = new ArrayList<String>();
list = getResult(...);
...
searchMap.put(differentkey, list);
}
Each time I am creating new List in loop. How to avoid creating new list in loop.
Simply don't create the List at all since it is not even used in the code you have shown.
List<String> list = getResult(...);
Why would you want to? It looks like you want as many lists as you have keys, given that your map is from a key to a list... so you want that many distinct lists.
In particular, you don't want to just clear the list on each iteration - as otherwise each map entry will refer to the same list.
Now you don't have to create an empty list which you then ignore - you can assign the value of getResult() immediately to the variable, in the declaration:
List<String> list = getResult(...);
That's likely to still be creating a new list on each iteration (unless getResult() returns an existing one) but that's probably what you want anyway.
Note that there's no benefit in declaring the list variable outside the loop - the variable declaration doesn't affect performance, but it's generally a good idea to limit a variable's scope as much as possible - so I'd keep the declaration where it is.
Try this instead:
Map<String, List> searchMap = new HashMap<String, List>();
for(int i=0; i<something.size(); i++){
List<String> list = getResult(...);
...
searchMap.put(differentkey, list);
}
There's no need to construct a new list.
Map<String, List> searchMap = new HashMap<String, List>();
List<String> list = new ArrayList<String>();
for(int i=0; i<something.size(); i++){
list = getResult(...);
...
searchMap.put(differentkey, list);
}
On the 4th line (list = getResult(...);) you're assigning a new object to your listvariable. So there's no need to create new list before that. Your variable will be replaced anyway.
for(int i=0; i<something.size(); i++){
List<String> list = new ArrayList<String>();
list = getResult(...);
is equivalent to
for(int i=0; i<something.size(); i++){
List<String> list = getResult(...);
But I'm not sure, whether you're really searching for this.

Categories