java打印字符串数组,如何在Java中的字符串数组中计算和打印重复的字符串?

这篇博客探讨了如何在Java中处理数组并找出重复的字符串。博主遇到一个挑战,需要遍历一个包含重复项的字符串数组,并将重复的单词打印在同一行,而没有重复的单词则单独显示。博主尝试了while和for循环,但未能找到合适的解决方案。最终,提出了一个排序数组然后使用双层循环检查相邻元素是否相等的策略,以此来打印重复项。
摘要由CSDN通过智能技术生成

I have a dilemma on my hands. After much trial and error, I still could not figure out this simple task.

I have one array

String [] array = {anps, anps, anps, bbo, ehllo};

I need to be able to go through the array and find duplicates and print them on the same line. Words with no duplicates should be displayed alone

The output needs to be like this

anps anps anps

bbo

ehllo

I have tried while, for loops but the logic seems impossible.

解决方案

Sort the array first then

for(int i = 0, i < array.length; i++){

String temp = array[i];

System.out.print(temp+" ");

for(int j = i+1; j < array.length; j++){

String temp2 = array[j];

if(temp.compareTo(temp2) == 0){

System.out.print(temp2+" ");

i++;

}

}

System.out.println();

}

or something similar...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值