Unlimited ArrayList 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 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.

Related

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.

get subset of elements from set in circular way [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 6 years ago.
Improve this question
Consider a set with like 100 elements.
Set<String> originalSet; //[1....100] size is 100
From originalSet, (m) subset of elements of some size(n) with some starting index(i) have to retrieved.
Example:
m = 4, n = 45, i = 1
Following have to be retrieved
subset1[1-45], subset2[46-90], subset3[91-35], subset4[36-80]
Whats the best way of doing this.
First of all, Set is unordered so it does not make sense to talk about indices etc. List would make more sense here.
Next, you have to be explicit about what you mean by "best". Performance on insertion? Random access? Creation of your n-from-i subset? These are important question to choose the implementation.
I think two primary options would be linked list with special handling of the last element in subList operation or an array-based list.
Assuming your set has some notion of order, you could write this with
Iterable<Iterable<String>> slices =
Iterables.limit(
Iterables.partition(
Iterables.skip(
Iterables‌​.cycle(originalSet),
i),
n),
m);
If you wanted a set out of this, you'd have to do a transform or something; if you have Java 8 that'd just be something like Iterables.transform(..., ImmutableSet::copyOf).

Is it possible to insert a number into a sorted array, time: log(n) [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 6 years ago.
Improve this question
I have a sorted array of integers (of size n) and would like to insert a new element into the array, is it possible to do so in O(log(n))? I need to keep an insertion and find computational complexity of O(log(n))).
Right now the only idea I have is to do binary search in order to find the desired index for insertion - this would take O(log(n)), but then I would have to create a new array and copy the entire cells which would take O(n).
EDIT:
It was solved by using an AVL Tree instead, that way any new elements added takes O(log(n)) and finding an element takes O(log(n))
"is it possible to do so in log(n)? " - in short no. From my recollections and experience inserting into an arbitrary position in an array is always O(N), almost by definition. If you want faster performance use something like the TreeMap class.

Time complexity of LinkedList, HashMap, TreeSet? [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 7 years ago.
Improve this question
I am a student of CS, learning about Java Collections.
At my school we have received a chart with with time complexity of different operations on data structures. There are some things in the chart that don't make sense to me.
Linked List
It says that time complexity for inserting at the end and finding the number of elements is implementation dependent. Why is that? Why isn't it O(n)?
HashMap
For the HashMap it says that tc for finding number of elements or determining whether the hashMap is empty has also tc implementation dependent.
TreeMap same goes for the TreeMap. According to the chart, the tc of the operation to determine the number of elements is implementation dependent. Which implementation do they mean?
Some implementations of LinkedList can keep a count of the number of elements in their list as well as a pointer to the last element for quick tail insertion. This means that they are done O(1) as it is a quick data access.
Similar reasoning can be followed for implementations of HashMap and TreeSet, if they keep a count of the number of elements with their data structure then functions such as isEmpty() and size() can be done O(1) also.

Does Collections.sort() shuffles equal priority elements? [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
Or just let them alone or throws an exception?
If the third one, how can I sort elements in ArrayList saving the old order of priority equal elements?
Thanks in advance.
From the documentation:
This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.
To save the order of the old arraylist, simply make a copy of the arraylist ahead of time. You can do this by saying:
List<Integer> oldListOrder = new ArrayList<Integer>(listToBeSorted);
Collections.sort(listToBeSorted);
Keep in mind the elements will reference the same objects in both lists. That may not affect you, it depends on what you do next.

Categories

Resources