Reputation:
I've written a code for removing the number
from an array. When I run my code, it happens not to remove the number
but instead gives me another number
. For example, when I pass number 44
in order to remove it, the ouput happens to be 3
instead of removing it. What could be the problem?
Given input:
2 5 33 44 3 8 6 7 4
Sample output:
2 5 33 3 3 8 6 7 4
Code:
public class RemoveAtIndex {
public static void main(String[] args)
{
int[] myArray = {2, 5, 33, 44, 3, 8, 6, 7, 4};
int[] output = removeFromIndex(myArray, 44);
for(int i = 0; i < output.length; i++)
{
System.out.print(output[i] + " ");
}
}
public static int[] removeFromIndex(int[] myArray, int num)
{
int[] resultArray = new int[myArray.length - 1];
for(int i = 0; i < myArray.length; i++){
if(myArray[i] != num){
resultArray[i] = myArray[i];
}
else
resultArray[i] = myArray[i+1];
}
return resultArray;
}
}
Upvotes: 2
Views: 47
Reputation: 9041
You're dealing with arrays of different size so you'll need two separate indexes when you're assigning the contents from the old array to the new array with the number of interest removed.
public static void main(String[] args) throws Exception {
int[] myArray = {2, 5, 33, 44, 3, 8, 6, 7, 4};
int[] output = removeFromIndex(myArray, 44);
System.out.println(Arrays.toString(output));
}
public static int[] removeFromIndex(int[] myArray, int num) {
// Index counter for the new array
int index = 0;
int[] resultArray = new int[myArray.length - 1];
for (int i = 0; i < myArray.length; i++) {
// Skip the element that matches the number wanting to be removed
if (myArray[i] == num) {
continue;
}
resultArray[index] = myArray[i];
index++;
}
return resultArray;
}
Results:
[2, 5, 33, 3, 8, 6, 7, 4]
Are you allowed to use Java 8? If so you can just do the removal process with one line and get the same results.
public static void main(String[] args) throws Exception {
int[] myArray = {2, 5, 33, 44, 3, 8, 6, 7, 4};
int[] output = removeFromIndex(myArray, 44);
System.out.println(Arrays.toString(output));
}
public static int[] removeFromIndex(int[] myArray, int num) {
return Arrays.stream(myArray).filter(n -> n != num).toArray();
}
Upvotes: 0
Reputation: 4401
Try:
public static int[] removeFromIndex(int[] myArray, int num)
{
int[] resultArray = new int[myArray.length - 1];
for(int i = 0; i < myArray.length; i++){
if(myArray[i] != num){
resultArray.push(myArray[i]);
}
else{
return myArray;
}
return resultArray;
}
}
You might have to include stack.push class
Upvotes: 0
Reputation: 5818
Your code will throw ArrayIndexOutOfBoundException
as the length of two arrays are different and you are using the same variable i
to iterate when assigning in the for-loop
So I've modified the code as i
for one array and j
for other array as
public static int[] removeFromIndex(int[] myArray, int num) {
int[] resultArray = new int[myArray.length - 1];
for (int i = 0, j = 0; i < myArray.length; i++) {
if (myArray[i] != num) {
resultArray[j++] = myArray[i];
}
}
return resultArray;
}
Upvotes: 1