Why ArrayList giving unordered output? - java

I have written java program, to add integer in ArrayList and remove that integer from ArrayList . but it not giving me proper result. here is my code..
public static void main(String args[])
{
ArrayList<Integer> a=new ArrayList<Integer>();
a.add(6);
a.add(7);
a.add(8);
a.add(9);
for(int i=0;i<=a.size();i++)
{
System.out.println("Removed Elements=>"+a.remove(i));
}
}
it giving me output as follows
Removed Elements=>6
Removed Elements=>8
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.remove(ArrayList.java:387)
at CollectionTemp.main(CollectionTemp.java:19)
why i am getting output like this?

Your array:
a[0]=6
a[1]=7 <-- i
a[2]=8
a[3]=9
Then you remove at 1, and i increments to 2:
a[0]=6
a[1]=8
a[2]=9 <-- i
Remember that array indexes start at 0, so the last element is at a.length - 1
You get your exception because the loop condition i <= a.size(), so at the final iteration:
a[0] = 7
a[1] = 9
2 <-- i

When you remove items from a list, or any collection, you either use the iterator, or you use a reverse loop like this.
for (int i = (a.size() - 1); i >= 0; i--) {
System.out.println("Removed Elements=>" + a.remove(i));
}
By going backwards, you avoid the incrementing by 2 problem that has been documented in the other answers.

for first iteration, a.remove(i) causes the element 7 to be removed which is returned by remove method.
For second iteration, the size of list is 3 and you are removing the element at index 2 which is 9. SO remove method returns 9.
In short
Iteration | Size of list | index being removed | element removed
----------+--------------+---------------------+----------------
1 | 4 | 1 | 7
2 | 3 | 2 | 9

If you want a forward loop to remove all elements you could use the following code:
while(!a.isEmpty())
{
System.out.println("Removed Elements=>" + a.remove(0));
}

Your problem is that, as you remove elements, you resize the ArrayList. However, your loop counter is not updated, so you iterate past the bounds of the ArrayList.
ArrayList.remove(index) removes the element from the array, not just the contents of the ArrayList, but it actually resizes your ArrayList as you remove items.
First you remove the 1st element of the ArrayList.
Removed Elements=>6
Here the list has been resized from size 4 to size three. Now the element at index 0 is 7.
Next, you step to the element at index 1. This is the number 8.
Removed Elements=>8
Here the ArrayList has been resized to length 2. So there are only elements at index 0 and 1.
Next you step to index 2.
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.remove(ArrayList.java:387)
at CollectionTemp.main(CollectionTemp.java:19)
There is no index 2, so you get an IndexOutOfBoundsException.

the index starts with 0 and ends with size - 1
your loop goes from 1 to size - 2

ArrayList indexes start at zero, but your loop starts removing at 1. After adding the elements your arraylist looks like this:
0 - 6
1 - 7
2 - 8
3 - 9
Because your loop starts counting at 1, you will first remove the element labeled 1, which is 7.
The list will then look like this:
0 - 6
1 - 8
2 - 9
Then the loop will remove the element labeled 2, which is now 9.
So the two mistakes are starting at 1 instead of 0, and incrementing the counter after something has been removed (all elements after the removed element will shift down).

In the first iteration , i starts from 1, so your second element is removed ie. 7. Now list has
6
8
9
Next iteration is 2, so third element 9 is removed.

Your output is correct : Here is the explaination.
When the loop is executed for the first time, value of i will be 1. and it execute the statement a.remove(1). after removing the value 7 which is at place a[1], '8will be ata[1]'s place. After that i is incremented and becomes 2 and it removes the element a[2] which is 9.

This is simple logic:
First Iteration i=1:
a[0]=6,
a[1]=7,
a[2]=8;
a[3]=9;
Remove a[i] i.e a[1] removes 7
Second Iteration i=2:
a[0]=6
a[1]=7
a[2]=9
Remove a[i] removes 9

The value of i in the loop goes through the following values
0
1
2
3
4
The array has index with values 0 , 1 , 2 , 3
The loop will run for values 0,1,2,3,4
The array is not displayed ordered bcoz when one value is removed the next value is available at index 0
At i=2 , the size of array is 2 and max index is 1 , and hence IndexOutofBound exception is encountered
use the following loop :
while(a.size()>0)
{
System.out.println("Removed Elements=>"+a.remove(0));
}

whenever you remove an element from arraylist it deletes element at specified location.and this should be noted each time the size of arraylist decreases.

I dont understand what are you trying to remove. If you wan to clear list just call clear() method. If you are trying to remove objects contained in list, than you should know that ArrayList contains objects, not primitive ints. When you add them your are doing the following:
a.add(Integer.valueOf(6));
And there are two remove methods in ArrayList:
remove(Object o) //Removes the first occurrence of the specified element from this list
and the one you are calling:
remove(int index) //Removes the element at the specified position in this list
May be you should call first one:
a.remove(Integer.valueOf(i));

Related

Deleting from Indexed Priority Queue (java)

I have an Indexed Minimum Priority Queue implemented as a heap. When deleting an indexed element, the code is:
public void delete(int i) {
if (i < 0 || i >= maxN) throw new IllegalArgumentException();
if (!contains(i)) throw new NoSuchElementException("index is not in the priority queue");
int index = qp[i];
exch(index, n--);
swim(index); // Why is this needed?
sink(index);
keys[i] = null;
qp[i] = -1;
}
The rest of the code can be found here: https://algs4.cs.princeton.edu/24pq/IndexMinPQ.java.html
Since pq[N] is the last element in pq[], and this is swapped with the element at pq[i] (which is to be deleted), wouldn't this mean that the value at pq[i] after the swap is larger or equal to pq[i] before the swap? The question is why do we have to call swim(i) at all and not just sink(i) alone? Under which specific conditions is there a need to call swim(i) after the swap?
(There are 3 arrays, qp[] and keys[] with corresponding indexes, and pq[] such that qp[pq[i]] = pq[qp[i]] = i.)
Since pq[N] is the last element in pq[], and this is swapped with the element at pq[i] (which is to be deleted), wouldn't this mean that the value at pq[i] after the swap is larger or equal to pq[i] before the swap?
No, that is not necessarily true. The only requirement for a valid min-heap is that a child cannot be smaller than its parent. While this means that the element in the first position is the smallest, it does not mean that the element in the last position is the largest. Consider the following heap:
1
10 2
15 18 5 3
16 17 19 20 7 8 6 4
pq[N] is 4 and yet there are lots of elements in that heap that are larger than it. Suppose we wanted to remove 15 by replacing it with 4. 4 is smaller than 10 and so would have to be moved up the tree (using swim).

Java: delete/pack method, can someone explain?

Can someone explain to me how this method works? I don't understand how they move in the array to delete the item index.
private void pack(int index) {.
for(int i =index; i<noOfItems-1; i++)
theItems[i]=theItems[i+1];
noOfItems--;
}
It simply shifts all elements, starting from the given index, one position up.
After that, it decreases the number of elements in the array.
For instance, calling the method with parameter 4, the value of the element with index 5 is moved to the element with index 4. The one on index 6 is moved to index 5, etc.
So, if you first had array
45, 78, 1, 45, 234, 856, 4323, 87, afterwards you have
45, 78, 1, 45, 856, 4323, 87
They are writing the next value of the array into it and decreased the array size.
For example,
int a[] = new int[] {1,2,3,4,5}
if I call pack(2) then the output of printing a is
{1,2,4,5,5}
I don't understand how they move in the array to delete the item index.
They don't. Java arrays are of fixed size, so the code simply copies all items starting at index up to noOfItems left by one element, and then records the fact that the number of items the array holds has reduced by one.
Here is an example: let's say that you start with this ten-element array
0 1 2 3 4 5 6 7 8 9
A B C D E F G H _ _
^
|
last item
Now you wish to delete the item at index 4. The algorithm would transform the array as follows:
0 1 2 3 4 5 6 7 8 9
A B C D F G H H _ _
^
|
last item
Note that the item at index 7 got copied, but did not get deleted. Its content does not really matter, because it's past the position of last item (i.e. noOfItems).
There are situations when this would be problematic - specifically, when you have large objects in the "dead" portion of the array, they may "linger" past the time when they should become eligible for garbage collection.
Beginning with index each value is shifted by one to the beginning of the array.
theItem[index] = theItem[index+1];
theItem[index+1] = theItem[index+2];
Because the value at index is never written to a new location, it is no longer accessible.
At the end the noOfItems is decremented by one because now there are one fewer elements in the array.
On the array [1,2,3,4,5,6] with noOfItems = 6 and calling pack(4) the following would happen:
[1,2,3,5,5,6]
[1,2,3,5,6,6]
noOfItems = 5
The last 6 in the array is not gone, but since every other bit of code (I guess) is using noOfItems to calc the end of the array it is no longer seen.

Java code length -1 meaning [duplicate]

This question already has answers here:
Does a primitive array length reflect the allocated size or the number of assigned elements?
(17 answers)
What does arrays.length -1 mean in Java?
(1 answer)
How do you determine the legal index range for an array?
(1 answer)
Closed 5 years ago.
The community reviewed whether to reopen this question last month and left it closed:
Original close reason(s) were not resolved
class Zillion {
private int zilly[];
public Zillion(int size){
zilly = new int[size];
}
public void increment(){
int i=zilly.length -1;
while(i>=0){
if(zilly[i]!=9){
zilly[i]+=1;
i=-1;
}
else{
zilly[i]=0;
i--;
}
}
}
I just got a basic java code like this and do not know what length -1 means at this part (int i=zilly.length -1;).
Can someone please explain this ?
The size of array is the number of elements in this array, but The first index of arrays is 0. For example :
int zilly[] = {1, 2, 3, 4}
In this example :
zilly.size return : 4
But zilly[4] not exist because the index of the first element is 0
zilly.[zilly.size - 1] return the last element (4).
Hope it helps.
nameOfTable.lenght return the number of element
the first index of Arrays start with 0 ==> (1st - 1 = 0) ,
so the last index is n - 1
int i=zilly.length -1; is the last index of the array, because arrays start at index 0.
A side note, the formatting of this code be improved to better portray what it's doing, for example the line that confused you could be.
int i = zilly.length - 1; simply spacing out the statement correctly makes it a bit easier to understand.
zilly.length -1 defines itself. Total array length (size) -1.
Since the first index of array starts with 0 so the last item is at position n-1 where n is the size of array.
In Java indexing of arrays starts from 0. Therefore the last element of arrays is accessed by zilly[zilly.length - 1]. If you try to access it this way:
zilly[zilly.length]
you will get an out of bounds exception.
The while loop in your code starts from the last index and iterates down the very first element.
zilly.length gives the length of the array, i.e. the number of elements in it.
zilly.length-1 gives the last index of the array because the count of the array begins from 0.
So basically you are iterating in a descending order, i.e. from the last element of the array to the first element of the array.
It means that we want to get the last index in the array.
And then we loop over the array from end to start.
zilly.length it's the array size - but the array's index starts from 0 so the last index is zilly.length-1

Duplicate for-loop practice, beginner needs advice to study

Understanding the -1 in the for loop, need detailed explanation with for and if lines of code included?
int[] array = { 2, 5, 1, 2, 3, 5 };
Arrays.sort(array);
// why does this start counting from 1, and if l put 0 it goes to error, out of bounds?
for (int i = 1; i < array.length; i++) {
if (array[i] == array[i - 1]) { // - 1?
System.out.print(array[i]);
}
}
There is nothing inherently wrong with it.
It just makes the iteration to start with i=1 up to the array's length, but since indexing in arrays are zero-based you have to offset it when getting the value.
That is why is array[i-1]
If you put i=0 then you also have to change the ending condition to array.length-1, and you have to access the values by array[i] in order to avoid going out of bounds.
For questions like this, take the code one line at a time and try to follow what it is doing.
The first thing you do here is to create an array, then sort it. After the Arrays.sort(array) call, your array will contain the following:
index 0 1 2 3 4 5
----------------------
contents 1 2 2 3 5 5
Remember, arrays are zero-indexed - that means the very first element is located at index 0. (The contents of the array don't matter, it's the index you want to pay attention to.)
Arrays cannot have indexes less then zero, and they also cannot have indexes greater than their length-1. So, for your example, that means you cannot use -1 as an index because it is less than 0. You also cannot use 6 as an index, since 6 is greater than the length of the array (6) minus one.
In the initialization of the for-loop, you set i equal to 1 for the first iteration. Though unconventional, it's perfectly legal. This simply represents which index we will start from in the array. So for the first iteration of the loop, array[i] will be pointing to the value 2, and array[i-1] points to the value 1.
index 0 1 2 3 4 5
----------------------
contents 1 2 2 3 5 5
i ^
i-1 ^
Now, what if you set i to start at index 0 instead? Well, then array[i] will point to the 0th index (value 1)... but what does array[i-1] point to?
index 0 1 2 3 4 5
----------------------
contents 1 2 2 3 5 5
i ^
i-1 ^
It's pointing to index -1. Since arrays can't have indexes of -1, this is causing your IndexOutOfBoundsException. Hopefully that makes sense.

java.lang.Exception: java.lang.ArrayIndexOutOfBoundsException: 7

I am trying to store each line of a file into a String array.
/*
*Input file
*2 1 1 1 1 1 1.33 1
*4 2 15 3 9 3 0.185
*/
String[][] data_array = new String[1][7];
int i = 0;
int j = 0;
//file read
StringTokenizer tokenizer =new StringTokenizer(line,delim);
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
data_array[i][j] = token;
j++;
}
But showing
java.lang.Exception: java.lang.ArrayIndexOutOfBoundsException: 7
But When I am trying with
String[][] data_array = new String[1][8];
I am not getting this exception instead I am getting below as the output.
2 1 1 1 1 1 1.33 1 null
4 2 15 3 9 3 0.185 null
In java, arrays are 0-based, that is the first element will have index 0 and the last element will have index n - 1 (where n is the length of the array).
As your array is declared [1][7], the last index will be number 6. Your first row contains 8 values, therefore you end up trying to load the 8th value (index 7) into an array containing 7 elements. Using index 7 results in an IndexOutOfBoundException.
Moreover, in your particular case, the first row of the input contains 8 elements, but the second row only contains 7 elements. If you try to load 7 values into the array containing 8 elements, the last one will be null. For the input you specified, with array declared as having length 8, the output would be:
2 1 1 1 1 1 1.33 1
4 2 15 3 9 3 0.185 null
(Note that I added extra spaces to indicate better how the array is populated.)
Further, it makes little sense to declare a two-dimensional array with the first dimension being 1 - it's the same as declaring a single-dimension array. What you probably want to do is have an array with first dimension referring to rows in the file and second dimension referring to values in rows.
The elements of an array with length i are numbered 0 to i-1.
I assume you are using blanks as delimeters. Is there a trailing blank at the end of the line? This would explain why you are getting one surplus token.
you must use array in your method not in the body of your class!
and arrays first element have index=> 0 and the lastest element have index=> N-1

Categories