Java 从字符串中删除重复项

Java 从字符串中删除重复项,java,string,Java,String,这是我的密码 public static String set_x_dates() { int noRecords = GlobalData.getNoRecords(); int n; String date = ""; if (noRecords <= 10) for (n = 0; n < noRecords; n++) date += Dates[n] + "-" + Month[n] + "

这是我的密码

   public static String set_x_dates()
   {
    int noRecords = GlobalData.getNoRecords();
    int n;
    String date = "";
    if (noRecords <= 10)
        for (n = 0; n < noRecords; n++)
            date += Dates[n] + "-" + Month[n] + "|";
    else {
        for (n = 0; n < noRecords; n++) {
            int gap = (int) (noRecords / 10);
            date += Dates[n] + "-" + Month[n] + "|";
            n++;
            if (n != noRecords)
                for (; gap > 0; gap--)
                    date += "|";
        }

    }
    return date;
}
我想要这个:

25-5|28-5|4-6|8-6|10-6|14-6|17-6|7-7|26-7| 

不要将日期连接到字符串,而是在循环记录时将日期添加到字符串中。集合不能包含重复项

然后在方法结束时,在集合上循环并构造一个要返回的字符串。

您可以组合一组字符串,这些字符串将在填充集合后连接起来


编辑:啊,dogbane先到了:p

下面是删除字符串中重复项的代码

输出:


你能把你得到的东西和你想要的东西贴出来吗?我得到的输出是25-5 | 28-5 | 4-6 | 8-6 | 14-6 | 17-6 | 7-7 | 7-7 | 7-7 1247 1247 1247 1247 1247 1247 1247 1247 1247 1247 1247-7 1247 1247 1247 1247 1247-7 1247 1247 1247 1247; 7 1247-7 1247-7 1247 1247 1247 1247 1247-7 1247-7-2625-5 | 28-5 | 4-6 | 8-6 | 10-6 | 14-6 | 17-6 | 7-7 | 26-7 |除了使用集合的建议之外,我认为你也可以在noRecords中作为设置| x|日期的参数,重新考虑集合| x|日期的名称,并在你的代码中排除重复行如果我这样做,我需要更改每个函数,目前,它们将日期作为字符串。下面我提供了访问set_x_dates()函数的代码。字符串mUrl+=“&chs=“+width+”x326”+”&chdl=b”+“&chxt=x,y”+”&chls=2 | 2”+“&chxl=0:|设置日期()”+1:“+set|y|u值()+”&chdlp=b”+“&chxt=x,y”+“&chls=2 | 2”+“&chxl 0:|”+set|设置日期()+set| 1:“+set|y|u值()+”&set|无收缩网格,你没有。正如我在回答中所说,在方法的末尾,将集合转换回字符串并返回它,这样就不必更改任何其他调用方法。
25-5|28-5|4-6|8-6|10-6|14-6|17-6|7-7|26-7|