java 数组 循环删除,Java使用循环从数组中删除重复项

博客内容涉及到一个Java编程问题,作者在尝试从整数数组中删除重复元素时遇到了困扰。提供的代码没有按预期工作,没有输出任何结果。解决方案是修正`if`语句中的条件判断,将`=`改为`==`,确保在检查布尔变量`duplicate`的值时使用正确的比较操作符。
摘要由CSDN通过智能技术生成

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值