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.
Related
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.
The input is {4,7,3,6,7} and The output is:
[81]
[40, 41]
[21, 19, 22]
[11, 10, 9, 13]
[4, 7, 3, 6, 7]
The recursive program I have for this is below, where I have added some print statements to understand the recursion:
import java.util.Arrays;
public class triangle_array {
public static void print(int a[])
{
if(a.length==0)
return;
int n=a.length;
int newa[]=new int[n-1];
for(int i=0;i<n-1;i++)
{
newa[i]=a[i]+a[i+1];
}
System.out.println("here1") ;
print(newa);
System.out.println("here2") ;
if(newa.length!=0)
System.out.println(Arrays.toString(newa));
}
public static void main(String args[])
{
int a[]={4,7,3,6,7};
print(a);
System.out.println(Arrays.toString(a));
}
}
I got the output as:
here1
here1
here1
here1
here1
here2
here2
[81]
here2
[40, 41]
here2
[21, 19, 22]
here2
[11, 10, 9, 13]
[4, 7, 3, 6, 7]
I am not able to completely understand this recursion,
From the above here statements, I understand that the print methos is being called recursively first, and when the condition fails, it returns to outside of print and goes to the line and prints "here2" 2 times and it verifies for the length of newa which is zero, until this point I understood, but in the next iterations how does the length of newa increase and how does the below condition become true, for the println statement ?
In general, if there are prints in a recursive method, all the prints that come before the recursive call will be printed in the order of increasing recursion depth, and all the prints that come after the recursive call will be printed in the reverse order.
public static void demoRecursion( int n, int depth ) {
if ( n <= 0 ) {
return;
}
System.out.println( "Before recursive call, depth " + depth );
demoRecursion( n - 1, depth + 1 );
System.out.println( "After recursive call, depth " + depth );
}
If you call this method using demoRecursion( 3, 1 ) you'll get:
Before recursive call, depth 1
Before recursive call, depth 2
Before recursive call, depth 3
After recursive call, depth 3
After recursive call, depth 2
After recursive call, depth 1
So it's not as if a was increasing in size. It's simply that at depth 1, you have a 5 item array, at depth 2, you have a 4 item array, at depth 3, you have 3 and so on.
So because of the reverse printing effect I demonstrated above, each depth`s array is printed after the array of the deeper level, which was shorter.
If you had printed the array before the recursive call, the prints would have been in decreasing order.
You need to think hard to understand it properly.
Since you don't understand how newa's size decreases,I'll explain that part. Firstly,print method is called from main. Then, 1st print starts executing,and creates newa with length 4 and stops at print(newa) while the 2nd print starts.
Then, 2nd print starts executing,and creates newa with length 3 and stops at print(newa) while the 3rd print starts.
Then, 3rd print starts executing,and creates newa with length 2 and stops at print(newa) while the 4th print starts executing.
Then, 4th print starts executing,and creates newa with length 1 and stops at print(newa) while the 5th print starts executing.
Then, 5th print starts executing,and creates newa with length 0 and stops at print(newa) while the 6th print starts executing.
Then, 6th print starts executing, and stops at return; as length is 0 at this point.
The 5th print continues and as the length is 0,it will not print anything.
The 4th print continues and prints newa (length is 1)
The 3rd print continues and prints newa (length is 2)
The 2nd print continues and prints newa (length is 3)
The 1st print continues and prints newa (length is 4)
At last,main prints the whole array.
The length of newa doesn't increase, it always decreases by one. The shortest one gets printed first because the recursive call is before the instruction to print the array. So it says "create the new shorter array, but then recursively handle it (which involves printing) first, then print yourself".
By the way, the way this is structured is kind of weird. Rather than print newa at the end of the print method, I think it would make more sense to print a (without the if around it), and then you also wouldn't need to print in the main method.
Basically the method is repeatedly making the array smaller by summing consecutive elements. For example:
4, 7, 3, 6, 7
|/ |/ |/ |/
11 10 9 13
|/ |/ |/
21 19 22
|/ |/
40 41
|/
81
The way it works is that on each call, it creates a new array that is one element smaller in size called newa, and each element newa[i] is calculated according to the formula newa[i] = a[i]+a[i+1]. Obviously we want to halt sometime, we don't want to keep recursing and get a StackOverFlowException, so we stop when the input array is empty. So, in psuedo-code, the whole thing is
print(a):
if a is empty
escape from function
else
create newa array of length a.length-1
newa[i] = a[i]+a[i+1] //fill the smaller array
print(newa) // print the smaller array
Your function begin by
if(a.length==0)
return;
So the function clearly stop when a is empty, but in that case, it stops when succession of a decreasing finish by giving a 0-length a. So if your original array have 4 entry, the function will call itself 3 times. 5 entry will call 4 times. And so on. And as each call may recall itself, 5 entry will call the functin,n giving an array of 4 entry, wich will give an array of 3 entry, untill an array of a single entry.
int newa[]=new int[n-1];
for(int i=0;i<n-1;i++)
{
newa[i]=a[i]+a[i+1];
}
this is the key: newa[] is one dimension less than a (n-1, and n is length of a). that's why on each line the array size increases by 1 (last called, first printed). This is the piece that provide each time an array smaller.
The for loop simply put on newa the sum of two neighbour value of a.
This is what it will give successively:
[4, 7, 3, 6, 7]
[4+7, 7+3, 3+6, 6+7] --> [11, 10, 9, 13]
[11+10, 10+9, 9+13] --> [21, 19, 22]
[21+19, 19+22] --> [40, 41]
[40+41] --> [81]
The less sized array is displayed first because when you call the function, it will call itself until the smallest array possible. And the structure of print make that the last called is the first printed, as last called function will be the first to finish, the other call won't finish untill the level below doesn't have finished. That's why you thought a was increasing, you must read those kind of function in the reverse order.
Regarding your print in order to debug. The long amount of "here1" is caused by the fact you print this before the function prints it output. That's why you got a lot of here1. then each function call prints it output.
And finally, regarding the last condition:
if(newa.length!=0)
When a is [81], newa will be an array of length 0, it's to ensure you can't print a 0-length array. So this condition is always true untill the last level.
Note that putting print(newa); under that condition and removing the first part that checks a isn't an array of 0 length won't change anything in the function
What would help you to understand would be to remove all println statements and instead put just one System.out.println(Arrays.toString(newa)) at the very start of the print method. The triangle will be inverted and it will make more sense.
I'm not asking anyone to solve this for me, I just need a little push because I have no earthly idea on where to begin with this. All I know is that I should implement collections in this and have a sort.
Write a method longestSortedSequence that returns the length of the longest sorted sequence within a list of integers. For example, if a variable called list stores the following sequence of values:
[1, 3, 5, 2, 9, 7, -3, 0, 42, 308, 17]
then the call: list.longestSortedSequence() would return the value 4 because it is the length of the longest sorted sequence within this list (the sequence -3, 0, 42, 308). If the list is empty, your method should return 0. Notice that for a non-empty list the method will always return a value of at least 1 because any individual element constitutes a sorted sequence.
Assume you are adding to the ArrayIntList class with following fields:
public class ArrayIntList
{
private int[] elementData;
private int size;
// your code goes here
}
Iterate the array, and increment the counter variable if the next element you process is larger then the last one.
If the next element is smaller, or the end of the array is reached, store the current counter value if its larger then the currently stored max value and reset the counter variable with 0.
Pseudo code:
Variable X: first item of list
Variable Y: length of sequence (initial: 1)
Variable Z: max length occurred (initial: 0)
Loop over the list starting from 2nd index
if item is higher than X
set X to item
add 1 to Y
else
if Y is higher than Z
set Z to Y
end if
set X to item
set Y to 1
end if
End-Loop
This method will restart the counter every time the sequence 'restarts', aka: it's no longer sorted. While the list is sorted it just adds 1 for each element that is in sorted order.
When the sequence stops being ordered it checks if the current sequence is longer than the longest sequence length so far. If it is, you have your new longest sequence.
Have you thought about a for loop and if else statements? i hope this doesn't give it away. think one element at a time.
Loop over your array and compare i element with i+1 element. Make a counter. While i is less than i+1 increment the counter, when i is greater than i+1 reset the counter.
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));
What is the use of .length here? What could be substituted with .length in the code below? I just wanted to understand the code better, Exams tomorrow! W00t!
public class twoDimension{
public static void main(String[] args) {
int[][] a2 = new int[10][5];
for (int i=0; i<a2.length; i++) {
for (int j=0; j<a2[i].length; j++) {
a2[i][j] = i;
System.out.print(" " + a2[i][j]);
}
System.out.println("");
}
}
}
You have declared an array of arrays of int type and initialized it.
10 inside the first square bracket says that you are going to store 10 values for the row and is the size of the array ‘n’ where n=10. The row length is 10.
5 inside the second square bracket says that you are going to store 5 values for the column and is the size of the array ‘n’ where n=5. The column length is 5.
In this case, you have one array with 10 locations
streetX[house_1]
streetX[house_2]
streetX[house_3]
streetX[house_4]
streetX[house_5]
streetX[house_6]
streetX[house_7]
streetX[house_8]
streetX[house_9]
streetX[house_10]
and inside each of the location is another 5 different locations
{house_i[room_number1], house_i[room_number2], house_i[room_number3], house_i[room_number4], house_i[room_number5]}
where i can represent any of 1 to 10
Think of it as 10 houses on a street. Each of the houses having 5 rooms. Each house will have a unique address to distinguish it from other houses on the street. In the same manner, each room in one house will be different from the rest. You can now keep different stuffs in each room.
The street is has an array of houses and each house has an array of room.
Therefore, you will have: You can think of a2 as streetX, i.e. a2 = streetX
streetX has 10 houses, therefore, there are 10 locations in a2. The size of a2 is 10 meaning that the length is 10. Therefore, a2.length = 10
NOTE: When you refer the array values, the index starts from 0 ‘zero’ to ‘n-1′. An array index is always a whole number and it can be a int, short, byte, or char.
house_i (where i ranges from 1 t0 10) but we usually count the index from 0. We'll have index 0 to 9. house_i is similar to a2[i]. There are 5 rooms, hence 5 locations. The size of any a2[i] (i.e. any house a2[0], a2[1], a2[2], a2[3] or a2[4])) is 5 meaning that the length is 5. Therefore, a2[i].length = 5
a2[house1][room1] a2[house1][room2] a2[house1][room3] a2[house1][room4] a2[house1][room5]
a2[house2][room1] a2[house2][room2] a2[house2][room3] a2[house2][room4] a2[house2][room5]
a2[house3][room1] a2[house3][room2] a2[house3][room3] a2[house3][room4] a2[house3][room5]
a2[house4][room1] a2[house4][room2] a2[house4][room3] a2[house4][room4] a2[house4][room5]
a2[house5][room1] a2[house5][room2] a2[house5][room3] a2[house5][room4] a2[house5][room5]
a2[house6][room1] a2[house6][room2] a2[house6][room3] a2[house6][room4] a2[house6][room5]
a2[house7][room1] a2[house7][room2] a2[house7][room3] a2[house7][room4] a2[house7][room5]
a2[house8][room1] a2[house8][room2] a2[house8][room3] a2[house8][room4] a2[house8][room5]
a2[house9][room1] a2[house9][room2] a2[house9][room3] a2[house9][room4] a2[house9][room5]
a2[house10][room1] a2[house10][room2] a2[house10][room3] a2[house10][room4] a2[house10[room5]
NOTE: When you refer the array values, the index starts from 0 ‘zero’ to ‘n-1′. An array index is always a whole number and it can be a int, short, byte, or char.
If n = 10, we'll have 0 ... 9
If n = 5, we'll have 0 ... 4
As an example, streetX[house1][room1] = a2[house1[room1] will become a2[0][0]
Eventually, you will have:
a2[0][0] a2[0][1] a2[0][2] a2[0][3] a2[0][4]
a2[1][0] a2[1][1] a2[1][2] a2[1][3] a2[1][4]
a2[2][0] a2[2][1] a2[2][2] a2[2][3] a2[2][4]
a2[3][0] a2[3][1] a2[3][2] a2[3][3] a2[3][4]
a2[4][0] a2[4][1] a2[4][2] a2[4][3] a2[4][4]
a2[5][0] a2[5][1] a2[5][2] a2[5][3] a2[5][4]
a2[6][0] a2[6][1] a2[6][2] a2[6][3] a2[6][4]
a2[7][0] a2[7][1] a2[7][2] a2[7][3] a2[7][4]
a2[8][0] a2[8][1] a2[8][2] a2[8][3] a2[8][4]
a2[9][0] a2[9][1] a2[9][2] a2[9][3] a2[9][4]
The outer for...loop will be repeated 10 times, remember, a2.length = 10 and the inner for ... loop is repeated 5 times, remember, a2[].length = 5
You initialized a2[i][j] = i, therefore for each inner loop, you will have the index of i (the house) will be the stored as the value or number of things in each room for the house.
Hence, when you execute the program, you will get the output or result below printed out.
Count the number of rows and columns and observe that on each line the values are the same.
0 0 0 0 0
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
6 6 6 6 6
7 7 7 7 7
8 8 8 8 8
9 9 9 9 9
For an array you can use [array_object_name].length or specify the size in number. E.g. a2.length or 10, or a2[i].length or 5 in you example program. Where i equals any of 0, 1, 2, 3, 4. Note that length in .length is a static variable ( You can read more about static variables from here http://download.oracle.com/javase/tutorial/java/nutsandbolts/variables.html and learn about static methods and static classes as well)
In summary, the length is the size of an array
You can learn more about array from here - http://javapapers.com/core-java/java-array/
What is the use of .length here?
no different from one dimensional array: to get number of elements in the array, if you treat two dimensional array as a table, then assuming the first .length is the row length, the second .length is the column length. get it?
What could be substituted with .length in the code below?
What do you see in the declaration (construction to be precise)?
a2.length is the number of rows. In this case the outer loop will iterate 10 times.
a2[i].length is the length of one specific row.