A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 不怕 中级黑马   /  2013-6-24 12:46  /  1119 人查看  /  10 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

List<string> str = new List<string> { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11","12","13" };

string[] temp = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};

想实现 temp 最后的结果是 {"11","12","13" } 即:排除相同元素后,剩下的

该如何写代码呢?

评分

参与人数 1技术分 +1 收起 理由
杞文明 + 1

查看全部评分

10 个回复

倒序浏览
for + if 就Ok了
回复 使用道具 举报
最后再加上remove 相同的数值
回复 使用道具 举报
哥们儿在上班时间偷做的,没上机验证过啊,不知道能不能通过;
  1. //
  2. import java.util.*;

  3. class StringList
  4. {
  5.         List<string> list = new List<string> { "1", "2", "3", "4", "5", "6", "7", "8", "9",

  6. "10", "11","12","13" };

  7.         String[] arr = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};

  8.         String[] finalArr = null;        //存放剔除相同元素后的数组;

  9.         public void convert()               
  10.         {
  11.                 List<String> asList = arr.asList();    //将arr数组转换为集合;
  12.                 //用列表迭代器迭代出list集合中的每一个元素,如果在asList集合中有相同元素,则移

  13. 除该元素,如果没有将该元素添加进asList集合中;
  14.                 for(ListIterator<String> li = list.listIterator(); li.hasNext();)
  15.                 {
  16.                         String temp = li.next();
  17.                         if(asList.contains(temp))
  18.                         {
  19.                                 asList.remove(temp);
  20.                         }
  21.                         else   asList.add(temp);   
  22.                 }
  23.                 //将转换后的asList集合转换为数组
  24.                 finalArr = asList.toArray(new String[asList.size()]);
  25.         }

  26.         public static void sop()        //打印最终修改过的数组;
  27.         {
  28.                 for(int i =0; i <= finalArr.length; i++)
  29.                 {
  30.                         System.out.println(finalArr[i]);
  31.                 }
  32.         }
  33. }

  34. public class MainAction
  35. {
  36.         public static void main(String [] args)
  37.         {
  38.                 StringList sl = new StringList();
  39.                 sl.convert();    //调用convert方法对数组进行处理;
  40.                 sl.sop();        //对最终修改过的数组打印输出;
  41.         }
  42. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
杞文明 + 1

查看全部评分

回复 使用道具 举报
  1. static void Main(string[] args)
  2.         {
  3.             
  4.             List<string> str = new List<string> { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11","12","13" };
  5.            
  6.             string[] temp = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
  7.            
  8.            
  9.             //想实现 temp 最后的结果是 {"11","12","13" } 即:排除相同元素后,剩下的

  10.             for (int i = 0; i < str.Count;i++ )
  11.             {
  12.                 for (int j = 0; j < temp.Length; j++)
  13.                 {
  14.                     if (str[i] == temp[j])
  15.                     {
  16.                         str.Remove(str[i]);

  17.                     }

  18.                 }

  19.             }

  20.             foreach (var item in str)
  21.             {
  22.                 Console.WriteLine("去掉相同的后,结果"+item);
  23.             }
  24.             Console.ReadKey();
  25.         }
复制代码
我写的这个不知道符不符合要求

点评

做了很多无用功,效率不高。我优化下。  发表于 2013-6-25 10:11

评分

参与人数 1技术分 +1 收起 理由
杞文明 + 1

查看全部评分

回复 使用道具 举报
本帖最后由 彭家贰小姐 于 2013-6-24 15:59 编辑

方法: 循环temp,str Contains 和 Remove即可
完整代码如下:
  1. using System;
  2. using System.Collections.Generic;

  3. namespace test11
  4. {
  5. internal class Program
  6. {
  7. /*List<string> str = new List<string> { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11","12","13" };
  8. string[] temp = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
  9. 想实现 temp 最后的结果是 {"11","12","13" } 即:排除相同元素后,剩下的
  10. */
  11. private static void Main(string[] args)
  12. {
  13. var str = new List<string> { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13" };
  14. string[] temp = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
  15. PrintLst(str);
  16. foreach (var t in temp)
  17. {
  18. if (str.Contains(t))
  19. {
  20. str.Remove(t);
  21. }
  22. }
  23. PrintLst(str);
  24. Console.ReadKey();
  25. }
  26. /// <summary>
  27. /// List打印
  28. /// </summary>
  29. /// <param name="lst">需处理List</param>
  30. public static void PrintLst(List<string> lst)
  31. {
  32. Console.Write("[ ");
  33. for (var x = 0; x < lst.Count; x++)
  34. {
  35. //如果不是最后一个元素则加','否则加']'
  36. if (x != lst.Count - 1)
  37. {
  38. Console.Write(lst[x] + ", ");
  39. }
  40. else
  41. {
  42. Console.WriteLine(lst[x] + " ]");
  43. }
  44. }
  45. }
  46. }
  47. }
复制代码


评分

参与人数 1技术分 +1 收起 理由
杞文明 + 1

查看全部评分

回复 使用道具 举报
来个超级简单的
  1.             foreach (string s in temp)
  2.             {
  3.                 str.Remove(s);
  4.             }
  5.             foreach (string s in str)
  6.             {
  7.                 Console.Write(s + "\t");
  8.             }
复制代码

点评

不是很理想啊,你怎么能保证str中就有这个元素?没有这个元素就会报错啊!  发表于 2013-6-25 11:30

评分

参与人数 1技术分 +2 收起 理由
杞文明 + 2 很给力!

查看全部评分

回复 使用道具 举报
本帖最后由 关关雎鸠 于 2013-6-25 14:08 编辑
  1. List<string> str = new List<string> { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13" };
  2.             string[] temp = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
  3.             int j = 0;

  4.             while(true) {               
  5.                 while(j < temp.Length) {
  6.                     //每次把str的第一个与temp中的元素进行比较,如相等就移除str的元素
  7.                     if(str[0] == temp[j]) {
  8.                         str.Remove(str[0]); //移除(每次移除一个元素,后面的元素就向前移动一个位置)
  9.                         j++;    //自加1
  10.                         break;  //跳出内层循环
  11.                         //(与str的第一个相等,因此没必要再去把temp的元素与第一个进行比较)
  12.                     }
  13.                 }
  14.                 //判断temp数组是否遍历完毕
  15.                 if(j >= temp.Length) {
  16.                     break;  //跳出外层循环
  17.                 }
  18.             }
复制代码
此代码要比“转达小朋友”好,他那个做了很多没必要的比较。而我这个改用while循环更简洁。如有不妥之处欢迎指正。

不过,题目有点局限性:如果temp中有与str不同的元素,这种方法不适合。

效果图:

QQ拼音截图未命名.png (5.58 KB, 下载次数: 0)

QQ拼音截图未命名.png
回复 使用道具 举报 1 0
不怕 中级黑马 2013-6-25 11:26:49
9#

这个确实漂亮!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马