Add Elements to ArrayList<ArrayList<String>> [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an ArrayList of ArrayList. How can I add an element to the list in specific position?
Example:
ArrayList<ArrayList<String>>=new ArrayList<>(list,list2,.....,listn);
I want to add an element to list2 for example.

You would get the second element and add to it: -
list.get(1).add(myString);
Remember it's 0 based though.

You need to get from your outer ArrayList your desired inner ArrayList first like
outerList.get(1); //this will return list2
then you can add to that inner arrayList some values like
outerList.get(1).add("your value"); //this will add element to list2

Assuming you named your list 'list':
list.get(1).add("x");
Where x is whatever you want to add to the second list.

Well, if you have list2, it's easy. You do :
list2.add("Moon");
If you only have the big list, which we shall call bigList, you do :
bigList.get(1).add("Moon");
Indexes in Java start at 0.

ArrayList<ArrayList<String>> myList=new ArrayList<>();
ArrayList<String> list1=new ArrayList<>();
ArrayList<String> list2=new ArrayList<>();
ArrayList<String> list3=new ArrayList<>();
list1.add("a") ; // add elements for list1
list2.add("b"); // add elements for list2
list3.add("c"); // add elements for list3
myList.add(list1); // add elements for myList
myList.add(list2);
myList.add(list3);
//Now I want to add new value for list2
myList.get(1).add("bb"); // indexes start with zero same as Arrays

Related

how to remove the duplicate innerlists from mainlist.in java? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 months ago.
Improve this question
list<list<Integer>ms2=new ArrayList<>();
for(int a=0;a<ms2.size()-1;a++)
{
List<Integer>arr=new ArrayList<Integer>(ms2.get(a));
for( int n=a+1;n<ms2.size();n++)
{
List<Integer>arr1=new ArrayList<Integer>(ms2.get(n));
if(arr.equals(arr1))
{
ms. remove(n);//here ms is the same list as the (ms2)just a copied version.
}
}
}
For example, consider a list of list in Java as:
[[1,1,1],[23,4,5],[1,1,1]]. Now, the output: [[23,4,5],[1,1,1]]. How do we do this, any idea?
The above code is not working for input: [[0,0,0,0],[0,0,0,0],[0,0,0,0][0,0,0,0]]. Output: [[0,0,0,0]], but for me it is throwing an error.
You can construct a Set from the list of lists to remove duplicates and convert it back to a List (if necessary).
List<List<Integer>> list = List.of(List.of(1,1,1),List.of(23,4,5),List.of(1,1,1));
Set<List<Integer>> set = new LinkedHashSet<>(list); // remove duplicates, preserving order
List<List<Integer>> list2 = new ArrayList<>(set); // convert back to List

How to iterate over items of list and delete duplicated ones while remain the original order? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Note: This question is for my assignment. So there are some rules which might seem to be not normal.
I just realized my question before doesn't make sense. So I edited it.
Assume that I have a list containing integers [1,2,3,4,1,2,1,5]
By using an iterator, how can I remove duplicated items while keeping the order of items undisrupted?
The expected result for above list should be [1,2,3,4,5]
And contain() method is not allowed to use.
What I currently came up with is the same as what Manash Ranjan Dakua answered in this question
How do I remove repeated elements from ArrayList?
public static void main(String[] args){
ArrayList<Object> al = new ArrayList<Object>();
al.add("abc");
al.add('a');
al.add('b');
al.add('a');
al.add("abc");
al.add(10.3);
al.add('c');
al.add(10);
al.add("abc");
al.add(10);
System.out.println("Before Duplicate Remove:"+al);
for(int i=0;i<al.size();i++){
for(int j=i+1;j<al.size();j++){
if(al.get(i).equals(al.get(j))){
al.remove(j);
j--;
}
}
}
System.out.println("After Removing duplicate:"+al);
}
But if I wish to use Iterator instead of for loops here? How can I achieve that(remove duplicated items while keeping order unchanged)?
If the question is not clear enough or too vague. Pls point out! I'll try to rephrase it.
Thanks for help!
You don't need to use Iterator to achieve that. A simple way is using LinkedHashSet which allows to contain unique elements only like HashSet (No Duplicates) and maintains insertion order.
Example:
List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,1,2,1,5));
Set<Integer> set = new LinkedHashSet<>();
set.addAll(list);
// or as suggested by assylias, Set<Integer> set = new LinkedHashSet<>(list);
System.out.println(list);
System.out.println(set);
Output:
[1, 2, 3, 4, 1, 2, 1, 5]
[1, 2, 3, 4, 5]
You can simply call .next() again which will allow you to move on to the next element in the array. However if you want to get a new iterator for the rest of the elements, it would be best for you to use .next() through the rest of the list you have, adding each element to a new data structure and then call .iterator() on that new data structure.

Set of Arraylists in Java [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
Set<ArrayList<Integer>> hs = new HashSet<ArrayList<Integer>>();
ArrayList<Integer> arr = new ArrayList<Integer>();
arr.add(1);
arr.add(2);
arr.add(3);
arr.add(4);
hs.add(arr);
ArrayList<Integer> arr1 = new ArrayList<Integer>();
arr1.add(4);
arr1.add(3);
arr1.add(2);
arr1.add(1);
hs.add(arr1);
System.out.println(hs.size());
The output I get is 2. I want to get 1 as both the arrayLists have the same elements. How can I achieve this?
In the second block, after creating arr1, you add the number 1 to 3 to arr again. Obviously, an ArrayList with 8 elements isn't equal to an empty ArrayList, so you'd have two members in the HashSet. If you fix the code to add the same elements to arr1, you'll get a HashSet with a size of 1.
In the code you pasted, you are adding 1, 2, 3, and 4 to the first list (arr) twice, and adding nothing to the second list (arr1). Thus arr1.equals(arr) is going to be false.
Fix the typo in your code so that arr1 contains the same elements as arr, and you will achieve your goal.
Even if the contents are same, both arraylists are different objects. You are bound to see 2 elements in the set.
Its like having two employees with exactly the same name.
Not sure if you can implement equals method for arraylists.

set first element of a List [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have the following List:
private String[] myArray = new String[]{"A","B","C"} ;
List myList = new ArrayList();
for (int i = 0; i < myArray.length; i++) {
myList.add(myArray[i]);
}
There is a method that returns an element of the myList
String returnValue=myMethod();
returnValue could be A or B or C
returnValue should be the first element of the List
myList.add(0, returnValue);
Issue is if returnValue="B" ,myList becomes {"B","B","C"}.
I could explicitly remove the returnValue and add it again to myList. But it seems rather redundant ,can anybody suggest a better design approach.
My Required Result would be {"B","A","C"}
As far as I understood, you want the returnValue to become the first element of the List, i.e. change the order.
This can be achieved by using:
Collections.swap(myList, 0, myList.indexOf(returnValue));
Since this does not use adding nor removing, it allows to simplify your entire code:
List<String> myList = Arrays.asList("A","B","C");
String returnValue=myMethod();
Collections.swap(myList, 0, myList.indexOf(returnValue));
Note that this changes the order of the remaining elements. If you want to retain the order of all elements but the one you move to the front of the list (like remove followed by add would do) you need to use rotate:
List<String> myList = Arrays.asList("A","B","C");
String returnValue=myMethod();
Collections.rotate(myList.subList(0, myList.indexOf(returnValue)+1), 1);
But using swap will be faster on large lists, so if you don’t need the remaining elements to retain the original order, using swap is recommended.
List.add(index, element) doesn't replace the first element in your List, it inserts and thereby it moves all the following elements. You would end up with 4 entries in your list:
{RETURNVALUE, "A","B","C"}
There's also a replacing variant: List.set(index, element). With that you would end uo with 3 entries in your list:
{RETURNVALUE,"B","C"}
If you want to keep your List all sorted, there's the simple utility method
Collections.sort(mylist);
that sorts your collection in respect of it's element's compareTo() method, which would be a simple String ordering in your case.
If your RETURNVALUE is B and you've inserted the element, you would end up with
{"A","B","B","C"}
If I good understand your problem, you should use set(int index, E element) method to replace given element in your list

identify input is from particular list within a nested list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
List<List<String>> listOflists = new List<List<String>>();
List<String> firstList = new List<String>();
firstList.add("NameX");
firstList.add("AgeX");
firstList.add("DesignationX");
listOflists.add(firstList);
List<String> secondList = new List<String>();
secondList.add("NameY");
secondList.add("AgeY");
secondList.add("DesignationY");
listOflists.add(secondList);
List<String> thirdList = new List<String>();
thirdList.add("NameZ");
thirdList.add("AgeZ");
thirdList.add("DesignationZ");
listOflists.add(thirdList);
Input:
My requirement is like "If i give Name? or Age? or Designation?" as input. I need to know that this particular input is from that list.
Example:
If i give "NameZ" as input, I need some clue that the input is from the thirdList.
Java collections have a method called, contains(item) which will search the collection for the specified item.
In this case the best thing to do is just go over these lists and use this method.
String searchStr = "age";
for (List<String> list : listOflists){
if (list.contains(searchStr)){
// inside this list, do something
}
}
If you are just looking for if an item is inside of collections, the fastest is by using a Set rather than a List, and would be more appropriate here.

Categories

Resources