会飞的五花肉 2010-12-22 09:11
浏览 270
已采纳

字符串元素数组求并集

举个例子 [code="java"]

List a =new ArrayList();
a.add("abc");
a.add("a");
a.add("cb");
a.add("f");
a.add("ab");
[/code]
求,a列表中元素的并集。。(说明:不考虑元素的顺序位置)
正确的结果应该为{abc,f}.
请大家帮忙! :x

  • 写回答

4条回答 默认 最新

  • Hywel_Zhang 2010-12-23 17:36
    关注

    [code="java"]class Test5Main {
    private static List getList(List lia) {
    List lis = new ArrayList();
    List tem = new ArrayList();
    lis.add(lia.get(0));
    tem.add(lia.get(0));
    for (String str : lia.subList(1, lia.size())) {
    setValue(lis, tem, str);
    }
    return lis;
    }
    private static void setValue(List lis, List tem, String str) {
    boolean isInsert = true;
    for (String s1 : lis) {
    int re = getState(s1, str);
    if (re == 1) {
    tem.remove(s1);
    }
    if (re == -1) {
    isInsert = false;
    break;
    }
    }
    if (isInsert) {
    tem.add(str);
    }
    lis.clear();
    lis.addAll(tem);
    }
    private static int getState(String s1, String s2) {
    // s2包含于s1
    if (isContain(s1, s2)) {
    return -1;
    }
    // s2包含s1
    else if (isContain(s2, s1)) {
    return 1;
    }
    return 0;
    }
    private static boolean isContain(String s1, String s2) {
    for (int i = 0; i < s2.length(); i++) {
    if (s1.indexOf(s2.charAt(i)) == -1)
    return false;
    }
    return true;
    }
    public static void main(String[] args) {
    List a =new ArrayList();
    a.add("abc");
    a.add("a");
    a.add("cb");
    a.add("f");
    a.add("ab");
    for (String string : getList(a)) {
    System.out.println(string);
    }
    }

    }[/code]
    给你个看看吧,不懂再讨论吧。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?