Recursively generation of vectors - java

I have a problem with a comprehensive understanding of recursion. I will copy the code and then we'll tell you what I learned from the whole thing and i will ask you my question.
static void generate(int index, int[] vector) {
if (index < 0) {
print(vector);
} else {
for (int i = 1; i <= 5; i++) {
vector[index] = i;
generate(index - 1, vector);
}
}
}
static void print(int[] vector) {
System.out.println(Arrays.toString(vector));
}
public static void main(String[] args) {
int size = 2;
int[] vector = new int[size];
generate(size - 1, vector);
}
At the beginning the index is 1 and after the for loop at position 1 the program record 1 after that the method is called recursively and the index became 0. On position 0 the program record 1. After that the program check the value of index and it is -1 (thats why it prints the first array). After that the index is still null and the program records only on position null but at position one the value is 1 without iteration. I can not understand how the program know that in position one the value is 1 ?

The combination of recursion and iteration may make it a bit hard to understand. You can always debug it in order to find out what it is doing.
First, generate (1, vector).
Then, in the iteration, vector[1] = 1 is called, and generate (0,vector) is called.
In this call, the iteration is called again, and when i=1 is set, vector[0] = 1 is set. generation(-1 ,vector) is called, the recursion breaks, and the content of the array is printed: [1, 1]
Next iteration: i=2; vector[0]=2 prints: [2,1]
Then: [3,1],[4,1],[5,1]
Back in generate (1, vector), vector[1] = 2 is set , and and generate (0,vector) is called ... and so ...

Related

sort an array, so that first and last element will form a "pair"

I have an array of objects which I want to sort by indices in the following order. I will always have an array size to the power of 2.
Example array size: 8. Indices: [0][1] [2][3] [4][5] [6][7]
After sort: [0][7] [1][6] [2][5] [3][4]
So basically alternating between first and last element which was not sorted yet.
I already thought of a way of doing it, and I can get the "pairs" but they are in the wrong order (and I think it would not work with any other to the power of 2 array?).
Here I'm using an int array and their indices as values to make it simpler for myself.
int[] array = new int[]{0,1,2,3,4,5,6,7};
int[] sortedArray = new int[8];
for(int i = 0; i < array.length; i+=2){
sortedArray[i] = array[i];
}
for(int i = 1; i < array.length; i+=2){
sortedArray[i] = array[array.length - i];
}
Output: [0][7] [2][5] [4][3] [6][1]
You can do this with a single loop. Consider the following algorithm:
int[] array = new int[]{0,1,2,3,4,5,6,7};
int[] sortedArray = new int[8];
for(int i = 0; i < array.length; i+=2){
sortedArray[i] = array[i/2];
sortedArray[i + 1] = array[array.length - i/2 - 1];
}
System.out.println(Arrays.toString(sortedArray)); // prints [0, 7, 1, 6, 2, 5, 3, 4]
This creates the final array by setting two values at a time:
every even index of the result array is mapped with the first values of the initial array
every odd index of the result array is mapped with the last values of the initial array
Here's a recursive approach to it, and improvements exist.
Motivation: Work your way through the array until you're left with only two values to compare. Once you're done, simply print them. Otherwise, print the value and continue slicing the array. We use two index locations to help us walk through the array, as each one governs its end of the array. We cease recursion when the difference between our start and end index is 1.
Steps to guard against silly things, like a zero-length array, I leave as an exercise for the reader.
public static void arrangeArray(int[] array) {
arrangeArray(array, 0, array.length - 1);
}
private static void arrangeArray(int[] array, int startIndex, int endIndex) {
System.out.printf("[%d][%d]\t", array[startIndex], array[endIndex]);
if(endIndex - startIndex > 1) {
arrangeArray(array, startIndex + 1, endIndex - 1);
}
}
You can extend a List and then Override the way this List works with indices so 0 remains 0 but 1 becomes physically a 2 in your internal array.
If you implement this object correctly, Collections.sort() won't see the difference. Then you can add your methods to get the internal Array for whatever you have to do.
The advantage of this approach is performance. You don't have to scramble the list yourself in a secondary step.
class swap
{
public static void main(String args[])
{
int arr[]={0,1,2,3,4,5,6,7};
int sorted[]=new int[8];
int end=7;
int i=1,j;
for(int k=0;k<8;k++)
{
sorted[k]=arr[k];
}
for(j=1;j<8;j=j+2)
{
sorted[j]=arr[end];
end--;
}
for(j=2;j<8;j=j+2)
{
sorted[j]=arr[i];
i++;
}
for(int k=0;k<8;k++)
System.out.println(sorted[k]);
}
}

How to insert a number into an array in java

Ok I am trying to do the following using an array.
Say I have one array
1 4 3 7 8
and at index 1 I want to place a 2 to get the following
1 2 4 3 7 8
How do I do this I think I have to make one array to keep everything before the index, one array to keep everything after the index.
And the add one more element to the array with everything before the index. The create a new longer array with everything before the index and everything after the index.
But I cannot seem to do it. This be what I tried.
//this program will test out how to replace an array with more stuff
public class Raton
{
public static void main(String[] args)
{
int[] gato={1,4,3,7,8};
int[] perro = new int[gato.length+1];
int[] biggie = new int[gato.length];
int index=2; //store item index 2
System.out.println("the contents of gato are ");
for(int i=0; i<gato.length;i++)
{
System.out.println(gato[i]);
}
for(int i=0;i<gato.length;i++)
{
if(i<index)
{
perro[i]=gato[i];
}
else
{
int red=0;
biggie[red]=gato[i];
red++;
}
}
//put two in the new place
for(int i=0;i<perro.length;i++)
{
System.out.println(" \n the contents of peero are " + perro[i]);
}
for(int i=0; i<biggie.length;i++)
{
System.out.println("\nthe contents of biggie are " + biggie[i]);
}
}
}
First you need to get both the new number and new index.
int newNumber = 2;
int newIndex = 1;
Create the new array with size+1 of old array
int[] gato = {1,4,3,7,8}; //old array
int[] perro = new int[gato.length+1]; //new array
Then keep track of of two counters. j for old array and i for new array.
int j = 0;
for(int i = 0; i<perro.length; i++){
if(i == newIndex){
perro[i] = newNumber;
}
else{
perro[i] = gato[j];
j++;
}
}
Here's a test run. This solution is assuming you have a constraint where you can only use arrays (not ArrayList or any other prebuilt classes in Java)
Use an ArrayList<Integer> instead of arrays, they allow easy insertion of new elements, and arraylists allow that at specific indices too.
int[] gato={1,4,3,7,8};
List<Integer> list = new ArrayList<>(Arrays.asList(gato));
list.add(1, 2);
I haven't tested this code, but something similar should do the trick.
public class Raton {
public static void main(String[] args) {
int[] originalArray = {1,4,3,7,8};
int[] modifiedArray = new int[originalArray.length + 1];
int index = 2;
for(int i = 0; i < (originalArray.length + 1); i++) {
if(i==1) {
modifiedArray[i] = index;
}
else if(i < 1){
modifiedArray[i] = originalArray[i];
}
else {
modifiedArray[i] = originalArray[i-1];
}
}
}
}
Try to look at the "before" and "after" arrays:
Before
┌─┬─┬─┬─┬─┐
│1│4│3│7│8│
└─┴─┴─┴─┴─┘
0 1 2 3 4
After
┌─┬─┬─┬─┬─┬─┐
│1│2│4│3│7│8│
└─┴─┴─┴─┴─┴─┘
0 1 2 3 4 5
One thing you already noticed is that you need a target array that is 1 bigger than the original array. That's correct.
But you have several problems in your program.
You copy all the parts that are before the index to perro. Since perro is your target array (the one bigger than the original), you have to make sure that everything gets copied to it. But instead, you only copy the parts that are before the index.
You want to place the 2 at index 1. But you wrote index=2. This means that both the 1 and 4 will be copied to perro consecutively. Your perro will look like this:
┌─┬─┬─┬─┬─┬─┐
│1│4│0│0│0│0│
└─┴─┴─┴─┴─┴─┘
0 1 2 3 4 5
and this means that you didn't put the 2 in the place you wanted it. That place is taken by the 4.
You try to copy the numbers after the index to biggie. But you are doing this using red. And in each iteration of the loop, you set red=0 again. So the only place in biggie that will change is 0, and that place will get all the numbers, and the last one will stay. So your biggie will be:
┌─┬─┬─┬─┬─┐
│8│0│0│0│0│
└─┴─┴─┴─┴─┘
0 1 2 3 4
You don't put the 2 anywhere!
You don't copy things from biggie to perro so you don`t get all the parts of the array together.
So let's look at our before and after arrays again:
Before
┌─┬─┬─┬─┬─┐
│1│4│3│7│8│
└─┴─┴─┴─┴─┘
0 1 2 3 4
After
┌─┬─┬─┬─┬─┬─┐
│1│2│4│3│7│8│
└─┴─┴─┴─┴─┴─┘
0 1 2 3 4 5
So first, we have to remember that the index we want to change is 1, not 2. Now look at the after array. You notice that there are three types of numbers:
Ones that stayed in the same place (the 1)
Ones that were added (the 2)
Ones that moved one place to the right (4,3,7,8)
How do we know which of the original numbers "stay" and which ones "move"? It's easy. The ones whose index is less than index (remember, it's 1!), that is, the one at index 0, stays.
All the others (including the one that is in the index itself!) have to move to a new place. The place is their old index + 1.
So your program should look like this:
Prepare an array whose size is one bigger than the original (like your perro).
Loop on all the indexes in the old array. Suppose the loop variable is i.
If i is less than the index, copy the number at index i from the original array to the new array at the same index, that is, at i.
For all other cases, copy the number at index i from the original array to the new array, but moved by one place. That is, i+1. There is no need for another variable or a ++ here.
When you finish that loop, your array will look like:
┌─┬─┬─┬─┬─┬─┐
│1│0│4│3│7│8│
└─┴─┴─┴─┴─┴─┘
0 1 2 3 4 5
Now don't forget to put your actual 2 there, at the index index!
Note: please give your variables meaningful names. I'm not sure, perhaps the names are meaningful in your native language, but biggie and red seem to be words in English, but they don't help us understand what these variables do. Try to use variable names that describe the function of the variable. Like original, target, temporary, nextIndex etc.
I think this is a clean and simple solution :
public static void main(String[] args) {
int[] gato = {1, 4, 3, 7, 8};
int index = 2; //store item index 2
System.out.println("the contents of gato are ");
for (int i = 0; i < gato.length; i++) {
System.out.println(gato[i]);
}
gato = Arrays.copyOf(gato, gato.length + 1);
for (int i = gato.length - 1; i > index; i--) {
gato[i] = gato[i - 1];
}
//put the element in the array
gato[index] = 2;
for (int i = 0; i < gato.length; i++) {
System.out.println(gato[i]);
}
}

Removing an element from an Array in java

So I am in the AP Computer Science class in High School so I'm not very experienced. I have a program to do that requires me to read in numbers from a file, put those numbers into an array and then remove all of the 0's from the array.
So if the numbers are: 0,2,4,6,0,5,3,5...
I would need to create an array: [0,2,4,6,0,5,3,5]
and then remove the 0's: [2,4,6,5,3,5]
I have to do this using arrays and I am not allowed to create a second array to do this. I have looked all over online and the Java API's to find a method that can remove an element from an array but I simply can't find one. If somebody has any idea of one that I can use or a direction to steer me in, your advice would be much appreciated.
THIS IS THE QUESTION WORD FOR WORD:
1. Write a program that reads a text file(compact.txt) and stores the integers in an array. Your instructor will provide this text file.
2. Write a method compact that removes all zeroes from the array, leaving the order of the elements unchanged. All local variables within this function must be scalar. In other words, you may not use a second array to solve the problem.
3. Do not solve the problem simply by printing out only the non-zero values in the array. The compact method must remove all zeroes from the array.
You could try the following: Since you cannot modify the length of the array, you can arrange it so you put all zeroes at the end of the array and with value -1 (this is optional, just to indicate they are zeroes).
public static void main(String[] args)
{
int[] arr = { 0, 1, 2, 0, 3, 0, 4, 0, 5, 6, 7 };
int[] arrWithoutZeros = compact(arr);
for (int i : arrWithoutZeros) {
System.out.println(i);
}
}
private static int[] compact(int[] arr)
{
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 0) {
int j = 0;
for (j = i; j < arr.length - 1; j++) {
arr[j] = arr[j + 1];
}
arr[j] = -1;
i--;
}
}
return arr;
}
Output:
1
2
3
4
5
6
7
-1
-1
-1
-1
Note: This meets the question requirements:
Leaves the order of elements unchanged (it changes its position, but not the order)
Don't use a second array
Not solved by just printing the non-zero elements
Removes zeros from array (they are now -1)
I'm not normally a fan of doing homework, but I found this one interesting and impossible as written, so here goes.
This is much like Christian's answer, but I'm using Integer instead of int so that I can set the 0s to null instead of some other integer. I've also avoided the extra loop he has to copy every remaining value down on every single 0, instead iterating the array once and then iterating the tail only once to set the null values.
public class ArrayCompact {
private static Integer[] ARRAY = { 1, 3, 5, 0, 7, 9, 0, 2, 0, 4, 6, 0, 8, -1, 0 };
public static void main( String[] args ) {
printArray( compact(ARRAY ));
}
public static Integer[] compact( Integer[] ints ) {
int j = 0;
for ( int i = 0; i < ints.length; i++ ) {
if ( ints[i] != 0 ) {
ints[j++] = ints[i];
}
}
for ( int i = j; i < ints.length; i++ ) {
ints[i] = null;
}
return ints;
}
public static void printArray( Integer[] ints ) {
for ( Integer i : ints ) {
System.out.print( i + " " );
}
}
}
Output 1 3 5 7 9 2 4 6 8 -1 null null null null null Technically I guess you could just not print the nulls, since that's not not printing 0s...
Another Edit:
(I think previous answer is still meaningful and I am keeping it at the end. This edit is mainly for suggestion specific to the homework requirement)
Base on the question, the compact logic treat 0 as something that is "meaningless" and need to be "removed". Therefore we don't really need some kind of special value after we "shrink" the array. Simply keeping it as 0 will help.
Apart from the "copy [i+1,end] to i" method, there is another (easier and possibly faster) method that you can do to "remove" the zeros.
Basically what you need is, iterate through the array. If you encounter a 0, then find the first non-zero value after that position, and swap the zero with that non-zero value.
Which looks like this in psuedo code:
for (i = 0; i < arr.length; i++) {
if (arr[i] == 0) {
for (j = i+1; j < arr.length; j++) {
if (arr[j] != 0) {
arr[i] = arr[j];
arr[j] = 0;
break;
}
}
}
}
// arr is "shrinked" here
Then it is your choice to return an actually shrinked copy of array, or simply return the "so-called-shrinked" array with 0s at the end.
Something for you to think of:
First, in Java, size of array is fixed, therefore, it is not possible to shrink array's size. Therefore, it is IMPOSSIBLE to have a result array with less elements without creating a new array
If it is fine for you to leave unused element at the end as some special values (e.g. -ve, or 0 etc), the removing element at position i from array essentially means:
copy array elements [i+1 to end] to position i, and replace arr[end] with special empty value
e.g. [1, 3, 5, 7, 9]
If I want to remove index 2, what I need to do is to copy element 3-4 to position 2:
[1,3,5,7,9] -> [1,3,7,9,9]
^^^ ^^^
and replace end element with some special value (e.g. -1 in this example):
[1,3,7,9,9] -> [1,3,5,7,-1]
Array copy can be done easily by using System.arrayCopy()
Have just seen update in your question.
Most of my answer is still valid, and here is some extra update with regards to your question:
If you are sure that no Integer.MIN will appear in your input, then use my above mentioned approach, and update the input array accordingly.
You may consider using Integer[] instead of int[], so that you can put null
This is the most "normal" approach, but given your requirement, this may or may not be valid. The question ask you to have only scalar LOCAL VARIABLES. Which implies to me that, if I don't create another variable, I can still return another array (seems that the question is only trying to stop you using another array in process of compacting). Just follow what I have mentioned above, however, instead of replacing the end position with some special value, just keep a local var which is array length. Whenever you remove an element (by copying [i+1, 0] to position i), decrement the array length var. At the end, return a new copy of "shrinked" array by using Arrays.copyOf(oldArray, newLength).
Here is a piece of psuedo-code for point 3:
int[] compact(int[] input) {
int arrSize = input.length;
int i = 0;
while (i < arrSize) {
if (input[i] == 0) {
copy input[i+1 to end] to input[i to end-1]
arrSize--;
} else {
i++;
}
}
return Arrays.copyOf(input, arrSize);
}

Remove every 3rd element in arraylist

I am trying to loop through an arraylist and gradually remove an element every 3 indices. Once it gets to the end of the arraylist I want to reset the index back to the beginning, and then loop through the arraylist again, again removing an element every 3 indices until there is only one element left in the arraylist.
The listOfWords is an array with a length of 3 that was previously filled.
int listIndex = 0;
do
{
// just to display contents of arraylist
System.out.println(listOfPlayers);
for(int wordIndex = 0; wordIndex < listOfWords.length; wordIndex++
{
System.out.print("Player");
System.out.print(listOfPlayers.get(wordIndex));
System.out.println("");
listIndex = wordIndex;
}
listOfPlayers.remove(listOfPlayers.get(listIndex));
}
while(listOfPlayers.size() > 1);
I have tried to implement for several hours yet I am still having trouble. Here's what happens to the elements of the arraylist:
1, 2, 3, 4
1, 2, 4
1, 2
Then it throws an 'index out of bounds error' exception when it checks for the third element (which no longer exists). Once it reaches the last element I want it to wrap around to the first element and continue through the array. I also want it to start where it left off and not from the beginning once it removes an element from the arraylist.
Maybe I have just missed the boat, but is this what you were after?
import java.util.ArrayList;
import java.util.Random;
public class Test {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<Integer>();
Random r = new Random();
//Populate array with ten random elements
for(int i = 0 ; i < 4; i++){
numbers.add(r.nextInt());
}
while(numbers.size() > 1){
for(int i = 0; i < numbers.size();i++){
if(i%3 == 0){//Every 3rd element should be true
numbers.remove(i);
}
}
}
}
}
You could move every third element to a temporary list then use List#removeAll(Collection) to remove the items when you finish each loop...until the master list was empty...
Lets back up and look at the problem algorithmically.
Start at the first item and start counting.
Go to the next item and increment your count. If there is no next item, go to the beginning.
If the count is '3', delete that item and reset count. (Or modulo.)
If there is one item left in the list, stop.
Lets write pseudocode:
function (takes a list)
remember what index in that list we're at
remember whether this is the item we want to delete.
loop until the list is size 1
increment the item we're looking at.
increment the delete count we're on
should we delete?
if so, delete!
reset delete count
are we at the end of the list?
if so, reset our index
Looking at it this way, it's fairly easy to translate this immediately into code:
public void doIt(List<String> arrayList) {
int index = 0;
int count = 0;
while(arrayList.size() != 1) {
index = index + 1;
count = count + 1; //increment count
String word = arrayList.get(index);//get next item, and do stuff with it
if (count == 3) {
//note that the [Java API][1] allows you to remove by index
arrayList.remove(index - 1);//otherwise you'll get an off-by-one error
count = 0; //reset count
}
if (index = arrayList.size()) {
index = 0; //reset index
}
}
}
So, you can see the trick is to think step by step what you're doing, and then slowly translate that into code. I think you may have been caught up on fixing your initial attempt: never be afraid to throw code out.
Try the following code. It keeps on removing every nth element in List until one element is left.
List<Integer> array = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
int nth = 3;
int step = nth - 1;
int benchmark = 0;
while (array.size() > 1) {
benchmark += step;
benchmark = benchmark > array.size() - 1 ? benchmark % array.size() : benchmark;
System.out.println(benchmark);
array.remove(array.get(benchmark));
System.out.println(array);
}
You could use a counter int k that you keep incrementing by three, like k += 3. However, before you use that counter as an index to kick out any array element, check if you already went beyond and if so, subtract the length of this array from your counter k. Also make sure, to break out of your loop once you find out the array has only one element left.
int k = -1;
int sz = list.length;
while (sz > 1)
{
k += 3;
if (k >= sz)
{
k -= sz;
}
list.remove(k);
sz --;
}
This examples shows that you already know right away how often you will evict an element, i.e. sz - 1 times.
By the way, sz % 3 has only three possible results, 0, 1, 2. With a piece of paper and a cup of coffee you can find out what the surviving element will be depending on that, without running any loop at all!
You could try using an iterator. It's late irl so don't expect too much.
public removeThirdIndex( listOfWords ) {
Iterator iterator = listOfWords.iterator
while( iterator.hasNext() ){
iterator.next();
iterator.next();
iterator.next();
iterator.remove();
}
}
#Test
public void tester(){
// JUnit test > main
List listOfWords = ... // Add a collection data structure with "words"
while( listOfWords.size() < 3 ) {
removeThirdIndex( listOfWords ); // collections are mutable ;(
}
assertTrue( listOfWords.size() < 3 );
}
I would simply set the removed to null and then skip nulls in the inner loop.
boolean continue;
do {
continue = false;
for( int i = 2; i < list.length; i += 3 ){
while( list.item(i++) == null && i < list.length );
Sout("Player " + list.item(--i) );
continue = true;
}
} while (continue);
I'd choose this over unjustified shuffling of the array.
(The i++ and --i might seem ugly and may be rewritten nicely.)

ArrayList and IndexOutOfBounds exception

So I'm getting an index out of bounds exception on some code i'm writing. What I don't understand is that I know for a fact that the index element I'm trying to work with exists.
Here's the code:
I have a constructor for an array list
public StixBoard(int number)
{
stixGame = new ArrayList<Integer>(number);
for (int i = 0; i < number; i++)
{
stixGame.add(i);
}
}
This block generates a random variable 1-3
public int computeMove()
{
int numberOfStix = (int) (3.0 * Math.random()) + 1;
return numberOfStix;
}
Really straight forward, now here I have a method that takes the parameter supplied and attempts to remove those number of elements from the array list. As you can see, the parameter must be between 1 and 3, and it must be less than or equal to the size of the array list. Otherwise, the user is prompted to enter another number
public boolean takeStix(int number)
{
boolean logicVar = false;
placeHolder = stixGame.size();
if ((number >= 1 && number <= 3) && number <= placeHolder)
{
for (int i = 0; i < number; i++)
{
stixGame.remove(i);
logicVar = true;
}
} else if (number > 3 || number > placeHolder)
{
do
{
System.out
.println("Please enter a different number, less than or equal to three.");
Scanner numberScan = new Scanner(System.in);
number = numberScan.nextInt();
} while (number > 3 || number > placeHolder);
}
return logicVar;
}
So as this program runs, the computeMove() method generates a random int (assuming the role of a computerized player) and attempts to translate that value to the number of indexes to be removed from the array list.
This ultimately brings me to this:
How many stix on the table? 4
|||||||||| 4 stix on the table
It's the computer's turn!
The computer chose 3
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 StixBoard.takeStix(StixBoard.java:38)
at StixGame.main(StixGame.java:55)
So as you can see, the array list is of size 4, but when the computer rolls a 3, (which should leave me with 1), I am left with this error. How does my array list go from being of size 4 indexes to size 2?
You iterate through your list, from beginning to end, and remove an element at each step. This makes all the elements in the list shift to the left.
First iteration: i = 0
[1, 2, 3]
Second iteration: i = 1
[2, 3]
Third iteration: i = 2
[2] -> IndexOutOfBoudsException. There is no index 2 in this list.
Iterate from the end to the beginning instead. That willmake it correct, and faster since the list won't have to copy all the elements from right to left.
The problem is in this loop:
for (int i = 0; i < number; i++)
{
stixGame.remove(i);
logicVar = true;
}
Once you remove the elements then list size is also decreased. If you start with list size 3 then in 3rd iteration, the index becomes 2 as initially 0 then 1 then 2 while size becomes 1 as intially 3 then 2 then 1. Hence IndexOutOfBoundException.
Try this:
for (int i = 0; i < number; i++){
stixGame.remove(0);//remove 0th index as previous element was removed
logicVar = true;
}
Look at it this way.
When you start your for-loop the ArrayList is of size x.
When you call remove() you take an element from the list. So the size is x-1.
But if you're constantly increasing the element you're removing, eventually you'll be removing an index that no longer exists. Remember, when you call remove() the contents of the array list are shifted. So if you had 0,1,2,3 before and removed 2. The list is 0,1,3. if you call remove(4) which was valid initially, you'll get an Out Of Bounds exception

Categories