user181415 - 2 years ago 66
Java Question

removing elements within an array

So I need to remove elements within an array based on user input. So if user enters 4 then all elements that have 4 within an array should be removed. So if the array is (3,2,1,1,0,4,1,2,8,9) that is a 10 member array. If user enters 1 to remove then it should print out (3,2,0,4,2,8,9), array should be shifted and the elements should be removed. How can this be possible in java. I know what I have doesn't make full sense, but I don't know how to tweak it so it will continue shifting the numbers. target is user input to remove element.
This is what I have so far:

``````    public static int [] randomInput(){
int [] list = new int[10];
for(int i = 0; i < list.length; i++){
list[i] = (int)(Math.random()*9) + 0;
}
return list;
}

public static int [] remove(int list[], int target){
int count = 0;
for(int i = 0; i < list.length; i++){

if(target == list[i]){
count++;

}

else{
list[i] = list[i];
}
}
int [] list1 = new int[list.length - count];
for(int i = 0; i < list.length - count; i++){
if(list[i] == target){
list1[i] = list[i+1];

}
else{
list1[i] = list[i];
}
}
return list1;
``````

}

Alright I'm going to try to help you, even though you really should just do this yourself. I am going to make a simple solution using only basic java.

``````import java.util.Scanner;

public class Main {
public static void main(String[] args){
int[] array = {3,2,1,1,0,4,1,2,8,9}; //Starting array duh.
Scanner scan = new Scanner(System.in); //Scanner to get what number we want to remove;

System.out.println("Enter a number you want to remove.");  //Setting the number
int numToRemove = scan.nextInt();                          //we want to remove

int temp = 0; //A int that will store the amount of times the number we want to remove is in the array.
for(int i = 0; i < array.length; i++){ //This loop just counts the
if(array[i] == numToRemove)        //number the number of times
temp++;                        //numToRemove is in the array.
}

int[] array2 = new int[array.length - temp];  //Making a new array that will be the size
//array - the number of numbers we are removing (temp).
int index = 0; //index for filling in the second array.

for(int i = 0; i < array.length; i++){
if(array[i] != numToRemove){  //If the number at i is not the number we are removing,
array2[index] = array[i]; //we add it to the new array at index,
index++;                  //and increase the index for the next number.
}
}

System.out.println("Your new array is: "); //The rest is just priting out the new array;
System.out.print("{");
for(int i = 0; i < array2.length; i++)
System.out.print(array2[i] + ", ");
System.out.println("}");
}
``````

}

I did NOT put it in the format you wanted.

I tried to make it very simple and comment everything I did. You can alter this code to fit your format, and change it to a method. Next time, do your homework earlier, and let this be a lesson learned. I mean I'm new around here so I'm glad to help if I can, but you will not always find someone to do it for you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download