Java使用循环从数组中删除重复项 [英] Java remove duplicates from array using loops

查看:287
本文介绍了Java使用循环从数组中删除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我当前的尝试,从包含整数的数组中删除重复项,但这没有任何结果。它只是什么都不打印。任何帮助将不胜感激。

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] + " ");}
    } 


推荐答案

尝试一下:

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] + " ");}
} 

您需要使用 == 而不是 = 在您的if语句中。

You need to use == not = in your if statement.

这篇关于Java使用循环从数组中删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆