Set of Arraylists in Java [closed] - java

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.

Related

Java array vs ArrayList vs List [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Im trying to understand the relationship between array, ArrayList, and List.
Say I wanted to remove duplicates by converting an array into an ArrayList then into a HashSet, then returned to an array.
int[] start = {1,2,3,4,5};
....
....
....
int[] result = .....
How would that work?
Array is a fixed length data structure. It is a continuous block of memory. Let say you have an array at x3000 with a length of 2. An array stay at x3000 and x3001. Remember, this is oversimplifying the concept, as element size and size per memory location will certainly affect the position of array's elements (not always end at x3001).
List is an abstract data type (ADT), the notable difference between list and array is list has no fixed length. There can be more differences but it may vary from case to case.
ArrayList is a ADT list, but implemented using array. It's like when you do problems that require constructing stack, queue, etc using array.
int[] array = {1, 2, 3, 4, 5};
for(int i : array) {
System.out.print(i+", ");
}
System.out.println();
ArrayList myArrayList = new ArrayList();
for(int num : array) {
myArrayList.add(num);
}
System.out.println(myArrayList);
HashSet<Integer> myHashSet = new HashSet<Integer>();
for(int i = 0;i<myArrayList.size();i++) {
myHashSet.add((Integer) myArrayList.get(i));
}
System.out.println(myHashSet);
This is an example of a normal int array converted to an ArrayList converted to a HashSet.
the relationship between array, ArrayList, and List.
An array is created with the [] and {} notation you showed in your question. It's not dynamically allocated.
A List is an interface, which is just a guide to what methods its implementations must respond to.
An ArrayList is an implementation of a List.

Unlimited ArrayList 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 3 years ago.
Improve this question
I need to make trivial array of strings in Java but I can not find a simple way to do it. I try to use List<String> myList = new ArrayList<>(); but implementation says this is limited to 10 items. The question is simple how to make array of strings without limit which is able to add new items and iterate in for cycle around it.
From the API Docs:
Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically
An ArrayList is just some methods wrapped around an array. That 'backing' array does have a capacity; the default is 10 as you mentioned. An ArrayList instance automatically replaces the backing array with a larger one as needed.

Trouble obtaining average to an array [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 4 years ago.
Improve this question
I am trying to get an average of an array and I found this code during my research.
int myArray[] = {1,2,3};
Arrays.stream(myArray).average();
System.out.println(Arrays.toString(myArray));
When I run it, the results come up as
[1, 2, 3]
I'm new at java and I feel like there's something obvious that I'm not seeing.
You are printing the original Array.
To print the calculated array add the next line:
System.out.println(Arrays.stream(myArray).average().getAsDouble());
Output:
2.0
You are just printing only the array, Instead, you can do this.
int myArray[] = {1,2,3};
System.out.println(Arrays.toString(Arrays.stream(myArray).average().getAsDouble()));
OR
int myArray[] = { 1, 2, 3 };
OptionalDouble ans = Arrays.stream(myArray).average();
System.out.println(ans.getAsDouble());
Here,OptionalDouble is a container object which may or may not contain
a double value. If a value is present, isPresent() will return true
and getAsDouble() will return the value.

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.

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

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

Categories

Resources