一个有关List中元素替换的问题

Neo_Xiao 2013-08-27 11:35:01
现在我有两个类

//trans
public class Trans
{
public string name;
public double fix_x;
public double fix_y;
public Trans(string a, double b, double c)
{
this.name = a;
this.fix_x = b;
this.fix_y = c;
}
}
//Nails
public class Nails
{
public string name;
public string nail;
}

这两个类分别用List<Trans>和List<Nails>装起来的,现在我要将List<Trans>中的name替换成List<Nails>中的nail,我写了一个实现方法,但是不知道为什么返回的还是原来的List<Trans>,求解!

public static List<Trans> change(List<Trans> a,List<Nails> b)
{
List<Trans> Transf_new = new List<Trans>();
Trans nail_new;

foreach (Trans nail in a)
{
string c = nail.name;
foreach (Nails net in b)
{
if (nail.name == net.nail)
c = net.name;
}
nail_new = new Trans(c, nail.fix_x, nail.fix_y);
Transf_new.Add(nail_new);
}
return Transf_new;
}
...全文
333 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Neo_Xiao 2013-08-28
  • 打赏
  • 举报
回复
感谢了各位,我已经找到原因,是我自己用错了字段,三楼给我的办法更简便.学习了,谢谢大家!
lele_nancy 2013-08-28
  • 打赏
  • 举报
回复
没有问题的啊 完全可以的。

 private void Form1_Load(object sender, EventArgs e)
        {
            List<Trans> trans = new List<Trans>();
            trans.Add(new Trans("a1", 200, 300)); 
            trans.Add(new Trans("a2", 1200, 1300)); 
            List<Nails> nails = new List<Nails>(); 
            nails.Add(new Nails("name1", "a1")); 
            nails.Add(new Nails("name2", "a2")); 
            //List<Trans> list = change(trans, nails); 
            //list.ForEach(x => Console.WriteLine("name:" + x.name + " fix_x:" + x.fix_x + " fix_y:" + x.fix_y));        
            var result = from p in trans 
                         from q in nails 
                         where p.name == q.nail 
                         select new { q.name, p.fix_x, p.fix_y }; 
            result.ToList().ForEach((x) => 
            { 
                Console.WriteLine("name:" + x.name + " fix_x:" + x.fix_x + " fix_y:" + x.fix_y); 
            });
        }

        //trans    
        public class Trans    
        {        
            public string name;        
            public double fix_x;        
            public double fix_y;        
            public Trans(string a, double b, double c)        
            {            
                this.name = a;            
                this.fix_x = b;            
                this.fix_y = c;        
            }    
        }    
        //Nails    
        public class Nails    
        {        
            public string name;        
            public string nail;
            public Nails(string a, string b)
            {
                this.name = a;
                this.nail = b;
            }
        }


        public static List<Trans> change(List<Trans> a, List<Nails> b)
        {
            List<Trans> Transf_new = new List<Trans>();
            Trans nail_new;

            foreach (Trans nail in a)
            {
                string c = nail.name;
                foreach (Nails net in b)
                {
                    if (nail.name == net.nail)
                        c = net.name;
                }
                nail_new = new Trans(c, nail.fix_x, nail.fix_y);
                Transf_new.Add(nail_new);
            }
            return Transf_new;
        }
threenewbee 2013-08-28
  • 打赏
  • 举报
回复
public static List<Trans> change(List<Trans> a,List<Nails> b)
{
return a.Join(b, x => x.name, y => y.name, (x, y) => new Trans(y.name, x.fix_x, x.fix_y)).ToList();
}

能用join就用join
join的原理是分别计算作为连接字段的hash,并且放入hash表中,所以它的算法复杂度是O(NlogN)
而你的写法包括ls的,相当于2层循环,复杂度是O(N^2)。
全栈极简 2013-08-28
  • 打赏
  • 举报
回复
or use linq:
List<Trans> trans = new List<Trans>();
            trans.Add(new Trans("a1", 200, 300));
            trans.Add(new Trans("a2", 1200, 1300));

            List<Nails> nails = new List<Nails>();
            nails.Add(new Nails("name1", "a1"));
            nails.Add(new Nails("name2", "a2"));

            var result = from p in trans
                         from q in nails
                         where p.name == q.nail
                         select new { q.name, p.fix_x, p.fix_y };
            result.ToList().ForEach((x) =>
            {
                Console.WriteLine("name:" + x.name + " fix_x:" + x.fix_x + " fix_y:" + x.fix_y);
            });
Neo_Xiao 2013-08-28
  • 打赏
  • 举报
回复
引用 2 楼 guwei4037 的回复:
or use linq:
List<Trans> trans = new List<Trans>();
            trans.Add(new Trans("a1", 200, 300));
            trans.Add(new Trans("a2", 1200, 1300));

            List<Nails> nails = new List<Nails>();
            nails.Add(new Nails("name1", "a1"));
            nails.Add(new Nails("name2", "a2"));

            var result = from p in trans
                         from q in nails
                         where p.name == q.nail
                         select new { q.name, p.fix_x, p.fix_y };
            result.ToList().ForEach((x) =>
            {
                Console.WriteLine("name:" + x.name + " fix_x:" + x.fix_x + " fix_y:" + x.fix_y);
            });
我尝试用4.0 但是不管用linq或者是join都还是一样.

            List<Trans> transf = new List<Trans>();
            transf = Read.trans(textBox_trans.Text);
            List<Nails> nailf = new List<Nails>();
            nailf = Read.nails(textBox_nail.Text);
            List<Trans> transf_new = new List<Trans>();
            transf_new = Read.change(transf, nailf);
            richTextBox1.Clear();
            foreach (Trans nail in transf_new)
            {
                richTextBox1.AppendText(nail.name + "\t");
                richTextBox1.AppendText(nail.fix_x + "\t");
                richTextBox1.AppendText(nail.fix_y + "\n");
            }
Neo_Xiao 2013-08-28
  • 打赏
  • 举报
回复
引用 3 楼 caozhy 的回复:
public static List<Trans> change(List<Trans> a,List<Nails> b) { return a.Join(b, x => x.name, y => y.name, (x, y) => new Trans(y.name, x.fix_x, x.fix_y)).ToList(); } 能用join就用join join的原理是分别计算作为连接字段的hash,并且放入hash表中,所以它的算法复杂度是O(NlogN) 而你的写法包括ls的,相当于2层循环,复杂度是O(N^2)。
这个看起来非常不错,可以我的.net还是用的2.0,还不支持Join
全栈极简 2013-08-27
  • 打赏
  • 举报
回复
我用了你的相关类和转换方法,并测试了一下,发现确实转换了。
static void Main(string[] args)
{
List<Trans> trans = new List<Trans>();
trans.Add(new Trans("a1", 200, 300));
trans.Add(new Trans("a2", 1200, 1300));

List<Nails> nails = new List<Nails>();
nails.Add(new Nails("name1", "a1"));
nails.Add(new Nails("name2", "a2"));

List<Trans> list = change(trans, nails);
list.ForEach(x => Console.WriteLine("name:" + x.name + " fix_x:" + x.fix_x + " fix_y:" + x.fix_y));
}

Java List去掉重复对象-java8 一、去除List重复的Stringpublic List removeStringListDupli(List stringList) { Set set = new LinkedHashSet<>(); set.addAll(stringList); stringList.clear(); stringList.addA

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧