c#list>如何循环添加值

悟空空9527 2019-08-14 09:40:03
public static List<List<double>> Get_Distance(List<zuoBiao> a1)
{
List<List<double>> ss = new List<List<double>>();
if (a1 != null && a1.Count > 1)
{
try
{
for (int i = 1; i < a1.Count; i++)
{
List<double> s1 = new List<double>();
for (int n = i; n < a1.Count; n++)
{
double result1 = Math.Sqrt(((a1[n].L - a1[n - 1].L) * (a1[n].L - a1[n - 1].L)) + ((a1[n].P - a1[n - 1].P) * (a1[n].P - a1[n - 1].P)));
s1.Add(result1);
}
ss.Add(s1);
}
}
catch(Exception ex)
{
}
}
return ss;
}



list<double>s1 传递的是引用
第二次的ss.Add(s1);
那么第一次的ss.Add(s1);中传递的s1会被第二次的s1覆盖

请问大神们怎么解决
...全文
1547 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
悟空空9527 2019-08-14
  • 打赏
  • 举报
回复
引用 8 楼 XBodhi. 的回复:
类似这样不久可以了吗
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<List<double>> list = new List<List<double>>();

            for (int i = 0; i < 100; i++)
            {
                List<double> subList = new List<double>();
                for (int j = 0; j < 100; j++)
                {
                    subList.Add(j + 0.0);
                }
                list.Add(subList);
            }
        }
    }
}
可行的 我的也是也可以的 是其他地方出了问题 谢谢
悟空空9527 2019-08-14
  • 打赏
  • 举报
回复
引用 7 楼 ManBOyyy 的回复:
按照你的想法可以這樣改 for (int i = 1; i < a1.Count; i++) { List<double> s1 = new List<double>(); for (int n = i; n < a1.Count; n++) { double result1 = Math.Sqrt(((a1[n].L - a1[n - 1].L) * (a1[n].L - a1[n - 1].L)) + ((a1[n].P - a1[n - 1].P) * (a1[n].P - a1[n - 1].P))); s1.Add(result1); } ss.Add(s1); } 改為 List<double> s1 = new List<double>(); for (int n = i; n < a1.Count; n++) { double result1 = Math.Sqrt(((a1[n].L - a1[n - 1].L) * (a1[n].L - a1[n - 1].L)) + ((a1[n].P - a1[n - 1].P) * (a1[n].P - a1[n - 1].P))); s1.Add(result1); } for (int n = i; n <s1.lenght; n++) { ss.Add(s1[n].tostring()); }
我需要把list<double>加到ss中 我之前的也是可行的 是其他地方吃了问题 谢谢
悟空空9527 2019-08-14
  • 打赏
  • 举报
回复
引用 6 楼 手在键盘敲很轻 的回复:
ss.Add(s1);这一步,改成 ss.Add( new List<double>(s1));
多谢 1、ss.Add(s1); 2、然后改变s1的引用,加到list ss中 3、之前加载到ss list中的值是保存下来的 4、改变s1 再加载到ss list 不影响之前已经加载到ss list 的s1。
XBodhi. 2019-08-14
  • 打赏
  • 举报
回复
类似这样不久可以了吗
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<List<double>> list = new List<List<double>>();

            for (int i = 0; i < 100; i++)
            {
                List<double> subList = new List<double>();
                for (int j = 0; j < 100; j++)
                {
                    subList.Add(j + 0.0);
                }
                list.Add(subList);
            }
        }
    }
}
ManBOyyy 2019-08-14
  • 打赏
  • 举报
回复
按照你的想法可以這樣改
for (int i = 1; i < a1.Count; i++)
{
List<double> s1 = new List<double>();
for (int n = i; n < a1.Count; n++)
{
double result1 = Math.Sqrt(((a1[n].L - a1[n - 1].L) * (a1[n].L - a1[n - 1].L)) + ((a1[n].P - a1[n - 1].P) * (a1[n].P - a1[n - 1].P)));
s1.Add(result1);
}
ss.Add(s1);
}
改為
List<double> s1 = new List<double>();
for (int n = i; n < a1.Count; n++)
{
double result1 = Math.Sqrt(((a1[n].L - a1[n - 1].L) * (a1[n].L - a1[n - 1].L)) + ((a1[n].P - a1[n - 1].P) * (a1[n].P - a1[n - 1].P)));
s1.Add(result1);
}
for (int n = i; n <s1.lenght; n++)
{
ss.Add(s1[n].tostring());
}
  • 打赏
  • 举报
回复
ss.Add(s1);这一步,改成 ss.Add( new List<double>(s1));
悟空空9527 2019-08-14
  • 打赏
  • 举报
回复
请问: 把ss.Add(s1); 然后改变s1的引用,加到list ss中的s1会改变吗
悟空空9527 2019-08-14
  • 打赏
  • 举报
回复
引用 3 楼 手在键盘敲很轻 的回复:
你把List<List<double>> ss = new List<List<double>>(); 声明在方法外面
问题是:我要
引用 3 楼 手在键盘敲很轻 的回复:
你把List<List<double>> ss = new List<List<double>>(); 声明在方法外面
8.44 6.99 5.12 3.25 6.23 3.14 5.33 7.68 9.65 4.25 6.33 5.14 4.22 我要把类似这种结构的数据加到ss中 每次循环s1是一行
  • 打赏
  • 举报
回复
你把List<List<double>> ss = new List<List<double>>(); 声明在方法外面
悟空空9527 2019-08-14
  • 打赏
  • 举报
回复
引用 1 楼 正怒月神 的回复:
那你直接这样就好啦 for (int n = i; n < a1.Count; n++) { List<double> s1 = new List<double>(); double result1 = Math.Sqrt(((a1[n].L - a1[n - 1].L) * (a1[n].L - a1[n - 1].L)) + ((a1[n].P - a1[n - 1].P) * (a1[n].P - a1[n - 1].P))); s1.Add(result1); }
这样无法把s1添加到ss中
正怒月神 2019-08-14
  • 打赏
  • 举报
回复
那你直接这样就好啦 for (int n = i; n < a1.Count; n++) { List<double> s1 = new List<double>(); double result1 = Math.Sqrt(((a1[n].L - a1[n - 1].L) * (a1[n].L - a1[n - 1].L)) + ((a1[n].P - a1[n - 1].P) * (a1[n].P - a1[n - 1].P))); s1.Add(result1); }

111,110

社区成员

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

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

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