This is my current attempt to remove duplicates from an array holding integers, but this does not give me any results. It simply prints out nothing. Any help would be greatly appreciated.
public static void duplicate(int numbers[], int size)
{
for (int i = 0; i < size; i++){
boolean duplicate = false;
int b = 0;
while (b < i){
if (numbers[i] == numbers[b])
duplicate = true;
b++;}
if (duplicate = false)
System.out.print(numbers[i] + " ");}
}
解决方案
Try this:
public static void duplicate(int numbers[], int size)
{
for (int i = 0; i < size; i++){
boolean duplicate = false;
int b = 0;
while (b < i){
if (numbers[i] == numbers[b])
duplicate = true;
b++;}
if (duplicate == false)
System.out.print(numbers[i] + " ");}
}
You need to use == not = in your if statement.