I am trying to increase the size of an ArrayList inside an ArrayList whenever a new value is added but I'm not sure on the correct way to do so. This is what I have right now:
public ArrayList<ArrayList<Integer>> outerList;
public int addValue(int value) {
int a = 0;
ArrayList<Integer> innerList = new ArrayList<Integer>();
if (noValue(value) == true) { // noValue checks if the value exists at a position in outerList and returns true if it's empty
innerList.add(value); // innerList has now size 1
outerList.add(innerList);
}
else {
a += 1;
innerList.add(value);
outerList.add(innerList);
}
return a;
}
But based on my tests, the size of innerList remains 1.
But based on my tests, the size of innerList remains 1.
This is because in your addValue() method you create a new innerList and add it to the list. Thus your outer ArrayList will consist of a lot of ArrayList's with only one object in them.
To change this you should use the get() method to get the inner ArrayList and then add the item to it. Something like:
outerList.get(index).add(value);
Where index is the index of the inner list you want to add the value to
You never get anything from outerList, all instances of innerList will always be newly created and then get one new entry added. Then that innerList is added to outerList and never touched again.
Related
I am looping through a list A to find X. Then, if X has been found, it is stored into list B. After this, I want to delete X from list A. As speed is an important issue for my application, I want to delete X from A without looping through A. This should be possible as I already know the location of X in A (I found its position in the first line). How can I do this?
for(int i = 0; i<n; i++) {
Object X = methodToGetObjectXFromA();
B.add(X);
A.remove(X); // But this part is time consuming, as I unnecessarily loop through A
}
Thanks!
Instead of returning the object from yhe method, you can return its index and then remove by index:
int idx = methodToGetObjectIndexFromA();
Object X = A.remove(idx); // But this part is time consuming, as I unnecessarily loop through A
B.add(X);
However, note that the remove method may be still slow due to potential move of the array elements.
You can use an iterator, and if performance is an issue is better you use a LinkedList for the list you want to remove from:
public static void main(String[] args) {
List<Integer> aList = new LinkedList<>();
List<Integer> bList = new ArrayList<>();
aList.add(1);
aList.add(2);
aList.add(3);
int value;
Iterator<Integer> iter = aList.iterator();
while (iter.hasNext()) {
value = iter.next().intValue();
if (value == 3) {
bList.add(value);
iter.remove();
}
}
System.out.println(aList.toString()); //[1, 2]
System.out.println(bList.toString()); //[3]
}
If you stored all the objects to remove in a second collection, you may use ArrayList#removeAll(Collection)
Removes from this list all of its elements that are contained in the
specified collection.
Parameters:
c collection containing elements to be removed from this list
In this case, just do
A.removeAll(B);
When exiting your loop.
Addition
It calls ArrayList#batchRemove which will use a loop to remove the objects but you do not have to do it yourself.
I'm writing a method that removes an element from an ArrayList of strings, if it is of a given length, and puts the removed String into an Array containing Strings, and I'm a bit confused at how to insert elements into the array after removing them from the List.
My ArrayList is called list, and it's an instance variable in the beginning of my program.
public String[] removeOfLength(String [] arr, int wordLength)
{
arr = new String[list.size()];
for(int i = 0; i < list.size(); i++)
{
if(list.get(i).length == wordLength)
{
list.remove(i);
//arr[i]
}
}
}
I removed the element, but I don't know how to insert it into the array.
The method is supposed to return a String array containing the removed Strings.
Instead of creating an array first, which has to be as long as the list itself, use a list again to hold the removed strings temporarily.
List<String> removedStrings = new ArrayList<String>();
for(int i = 0; i < list.size(); i++)
{
if(list.get(i).length == wordLength)
{
removedStrings.add(list.remove(i));
}
}
Then just convert the list into an array when the method ends
return removedStrings.toArray(new String[removeStrings.size()]);
Just assign the value at a certain index.
Also the remove method returns the value removed from the list.
arr[I]=list.remove(I);
Also you need to return arr at the end of the method. And if the method calling this one expects the array that its providing as an argument to have the elements it won't because you are assigning it a new reference at the beginning.
Also, the array will not fill like a list, there will be gaps if it doesn't remove every element from your list, arrays aren't smart like ArrayLists.
Since array is a fixed length structure and you have to specify the length at the creation, you cannot directly insert removed element to a new array because you don't know how many elements will be there in array.
Instead, you can use the arraylist to keep removed elements temporarily and at the end of the iteration, populate those to a new array (because at that point, you know the length of the array using number of elements in your temporary arraylist).
I am new to Java. I apologize if I ask a simple thing.
I wrote the below code but it seems that it doesn't initialize properly. because when I print the size of list1 it show the size = 0!! However, it should be 4!
public static class MyClass{
public List <Integer> list1
// Class Constructor
public MyClass(int n){
list1 = new ArrayList <Integer> (n);
System.out.println("Size = " + list1.size() );
// prints Size = 0 !!!why???
}
public void init(int n){
for(int cnt1 = 0; cnt1 < list1.size(); cnt1++){
list1.set(cnt1 , cnt1);
}
}
...}
public static List<Integer> Func1(int n){
MyClass = new myclass (n);
myclass.init(n);
... }
public static void main(String args[]){
int n = 4;
result = Func1 (n);
...}
Why the size of the list1 is 0? It should be 4, because I pass 4 to Func1, and then it creates MyClass object with size n.
I would be thankful if someone can help me about this problem.
Array lists in Java have both a size and a capacity.
Size tells you how many items are there in the list, while
Capacity tells you how many items the list can hold before it needs to resize.
When you call ArrayList(int) constructor, you set the capacity, not the size, of the newly created array list. That is why you see zero printed out when you get the size.
Having a capacity of 4 means that you can add four integers to the list without triggering a resize, yet the list has zero elements until you start adding some data to it.
You have used the ArrayList constructor that determines its initial capacity, not its initial size. The list has a capacity of 4, but nothing has been added yet, so its size is still 0.
Quoting from the linked Javadocs:
Constructs an empty list with the specified initial capacity.
Also, don't use set to add to the list. That method replaces an element that is already there. You must add to the list first (or use the other overloaded add method).
I am attempting to search through an array list to find a value (which may reoccur) and remove all instances of that value. I also would like to remove from a separate array list, values that are at the same location. Both ArrayLists are ArrayList<String>.
For example I am looking for the number 5 in ArrayList2:
ArrayList 1 ArrayList2
cat 1
pig 2
dog 5
chicken 3
wolf 5
Once I find the number 5, in both locations, I would like to remove dog and wolf from ArrayList1. My code has no errors but it doesn't seem to be actually removing what I am asking it.
//searching for
String s="5";
//for the size of the arraylist
for(int p=0; p<ArrayList2.size(); p++){
//if the arraylist has th value of s
if(ArrayList2.get(p).contains(s)){
//get the one to remove
String removethis=ArrayList2.get(p);
String removetoo=ArrayList1.get(p);
//remove them
ArrayList2.remove(removethis);
ArrayList1.remove(removetoo);
}
}
When I print the arrayLists they look largely unchanged. Anyone see what I am doing wrong?
When you are both looping and removing items from an array, the algorithm you wrote is incorrect because it skips the next item following each removal (due to the way in which you increment p). Consider this alternative:
int s = 5;
int idx = 0;
while (idx < ArrayList2.size())
{
if(ArrayList2.get(idx) == s)
{
// Remove item
ArrayList1.remove(idx);
ArrayList2.remove(idx);
}
else
{
++idx;
}
}
If you want to iterate over a collection and remove elements of the same collection, then you'll have to use an Iterator, e.g.:
List<String> names = ....
List<Integer> numbers = ....
int index = 0;
Iterator<String> i = names.iterator();
while (i.hasNext()) {
String s = i.next(); // must be called before you can call i.remove()
if (s.equals("dog"){
i.remove();
numbers.remove(index);
}
index++;
}
EDIT
In your case, you'll have to manually increment a variable to be able to remove items from the other List.
You could use two iterators:
Iterator<String> i1 = arrayList1.iterator();
Iterator<Integer> i2 = arrayList2.iterator();
while (i1.hasNext() && i2.hasNext()) {
i1.next();
if (i2.next() == s) {
i1.remove();
i2.remove();
}
}
Though as has been pointed out yet, it would probably be easier to use a map.
I think the contains method compares the two objects. However, the object "s" is different from the object in the ArrayList. You should use typed arrays (i.e. ArrayList) and make sure to compare values of each objects, not the objects themselves ...
You should declare your list as follows -
List<String> list1 = new ArrayList<String>();
//...
List<Integer> list2 = new ArrayList<Integer>();
//...
And instead of contains method use equals method.
Also to remove while iterating the lists use Iterator which you can get as follows -
Iterator<String> it1 = list1.iterator();
Iterator<Integer> it2 = list2.iterator();
//...
You might want to check the indexOf() method of ArrayList, but you have to be careful when removing from a list while iterating on it's elements.
Here's a straight forward solution:
List<Integer> origNums = new ArrayList<Integer>(nums);
Iterator<String> animalIter = animals.iterator();
Iterator<Integer> numIter = nums.iterator();
while (animalIter.hasNext()) {
animalIter.next();
// Represents a duplicate?
if (Collections.frequency(origNums, numIter.next()) > 1) {
// Remove current element from both lists.
animalIter.remove();
numIter.remove();
}
}
System.out.println(animals); // [cat, pig, chicken]
System.out.println(nums); // [1, 2, 3]
I agree with Makoto, using Map maybe more beneficial. If you will be searching only using the values of ArrayList2, then you have multiple values for one key.
For example, 5 refers to dog and wolf. For this you can add a list of values to the key - 5.
HashMap aMap = HashMap();
ArrayList key5 = new ArrayList();
key5.add("dog");
key5.add("wolf");
aMap.put(5, key5);
So when you need to remove all values for 5, you do
aMap.remove(5);
And it will remove the list containing dog and wolf.
I set an array of integer like below
int[] a = new int[11111];
//if I set
a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;
I want a method such that it gives me
4 but 11111.
Is there any method which I can use?
You should look into using an ArrayList
ArrayList<Integer> myList=new ArrayList<Integer>();
myList.add(1);
myList.add(2);
myList.add(3);
System.out.println("Size:"+myList.size());
Well, the following method will do what you asked for:
public int m() {
return 4;
}
On the assumption that you want a method that takes an array, and returns the greatest index that has been populated - you're right that the a.length only tells you the size of the array, i.e. the number of cells allocated.
This is going to be harder than you might expect, especially with an int array. Those unassigned cells are initialised to a value of 0. If you might actually use zero values in your array, then there is absolutely no way to tell whether the value in a cell is the "default" zero or one that you've set yourself.
If the array can't have zero values in it, then you'd need to loop over its entire length, checking for the highest index with a corresponding non-zero value; something like this:
public int dynamicLength(int[] a) {
int max = -1;
for (i = 0; i < a.length; i++) {
if (a[i] != 0) max = i;
}
return max;
}
Even then this might not be ideal, since arrays can be sparsely populated. Do you want the count of assigned indices, or the index of the highest assigned index?
The short answer is almost certainly "use an ArrayList".
When you do
int[] a = new int[11111]
It creates an array with 11111 elements and as it is int it will assign it to default value that is 0 so you have array with all values set.
You should move to List
You should use an ArrayList if the size of the array is changing. There is little performance difference.
See here for how to use one. See here for the API also.
I understand that you only want the assigned elements to be counted but it would be safer at runtime and simpler to use an ArrayList. The ArrayList class just wraps a Java array and handles the changing size for you. You can get the size by calling the size() method on the ArrayList.
See this example using a for-each loop if you want to iterate over the elements:
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1); //size is 1
list.add(2); //size is 2
list.add(3); //size is 3
list.add(4); //size is 4
for(Integer n : list)
System.out.println(n);
An ArrayList uses an iterator and the for-each loop uses it to iterate over the ArrayList. Makes life much simpler.
As suggested above, using a List is probably the right answer. However, in the interest of solving the original problem, you could try this instead:
Integer[] foo = new Integer[11111];
foo[0] = new Integer(1);
foo[1] = new Integer(2);
foo[2] = new Integer(3);
foo[3] = new Integer(4);
and create a method that counts non-null values:
public static int countItems(Integer[] array) {
int count = 0;
for (Integer i : array) {
if (i != null) {
count++;
}
}
return count;
}
Of course, this will be a pain to manage as you would need to nullify any items no longer needed. It also raises the question of whether you would accept "holes" in your array, e.g. null values amongst non-null values. My example counting function above would accept such holes.
So, yes. Use a List.
You can create a method which calculates the non-0 elements of the array using a for/while loop.