i'm trying copy an array without a specified element. Let's say I have the following arrays:
int[] array = {1,2,3,4,5,6,7,8,9};
int[] array2 = new int[array.length-1];
what I want is to copy array to array2 without the element containing the int "6" so it will contain "{1,2,3,4,5,7,8,9}"
I only want to use for loops and this is what I have so far but it doesnt work
int[] array= { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] array2= new int[array.length - 1];
int remove = 6;
for (int i = 0; i < array2.length; i++) {
if (array[i] != remove) {
array2[i] = array[i];
} else {
array2[i] = array[i + 1];
i++;
}
}
for (int i = 0; i < array2.length; i++) {
System.out.println(array2[i]);
}
Thanks
int j = 0;
int count = 0; //Set this variable to the number of times the 'remove' item appears in the list
int[] array2 = new int[array.length - count];
int remove = 6;
for(int i=0; i < array.length; i++)
{
if(array[i] != remove)
array2[j++] = array[i];
}