- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我可以用我目前正在做的方式构建字典,但我想知道是否有更好更快的方法。我正在尝试使用 LINQ SelecMany 语句但遇到了问题。
var replyChildren = reply.Children[0];
Dictionary<int, string> slowLog = new Dictionary<int, string>();
for (int i = 0; i < replyChildren.Children.Count(); i++)
{
var ithReplyChildren = replyChildren.Children[i];
for (int j = 0; j < 4; j++)
{
if (j == 3)
{
var jthReplyChildren = ithReplyChildren.Children[j];
for (int k = 0; k < jthReplyChildren.Count; k++)
{
serverInfo += jthReplyChildren.Children[k].TextData; //+ "::";
}
break;
}
serverInfo += ithReplyChildren.Children[j].TextData + ":";
}
slowLog.Add(i, serverInfo);
serverInfo = "";
}
Reply 将在内部(第 0 个元素)包含子元素,然后在其中包含子元素,依此类推。第二个 for 循环将只有 4 个子循环,而第 4 个 for 循环必须更深入一步并获取它的子循环。
最佳答案
第一步:停止使用字典。我们已经有了一个数据结构,可以将密集的、从零开始的整数范围映射到数据上:List<T>
.重构:
static string GetServerInfo(Thing thing)
{
string serverInfo = "";
for (int j = 0; j < 4; j++)
{
if (j == 3)
{
var jthReplyChildren = thing.Children[j];
for (int k = 0; k < jthReplyChildren.Count; k++)
{
serverInfo += jthReplyChildren.Children[k].TextData; //+ "::";
}
break;
}
serverInfo += thing.Children[j].TextData + ":";
}
return serverInfo;
}
现在你的主程序是:
List<string> slowLog = reply.Children[0].Children.Select(GetServerInfo).ToList();
现在我们已经消除了一个循环并将问题简化为制作GetServerInfo
看起来没那么可怕。
展开外层循环:
static string GetServerInfo(Thing thing)
{
string serverInfo = "";
serverInfo += thing.Children[0].TextData + ":";
serverInfo += thing.Children[1].TextData + ":";
serverInfo += thing.Children[2].TextData + ":";
var fourthChild = thing.Children[3];
for (int k = 0; k < fourthChild.Count; k++)
serverInfo += fourthChild.Children[k].TextData;
return serverInfo;
}
太好了,现在我们只剩下一个循环了。消除那个循环:
static string GetServerInfo(Thing thing)
{
string serverInfo = "";
serverInfo += thing.Children[0].TextData + ":";
serverInfo += thing.Children[1].TextData + ":";
serverInfo += thing.Children[2].TextData + ":";
serverInfo += string.Join("", thing.Children[3].Children.Select(x => x.TextData));
return serverInfo;
}
我们也可以为其他人使用 Join:
static string GetServerInfo(Thing thing)
{
string firstThree = string.Join("", thing.Children.Take(3).Select( x => x.TextData + ":"));
string fourth = string.Join("", thing.Children[3].Children.Select(x => x.TextData));
return firstThree + fourth;
}
进一步减少:
static string GetServerInfo(Thing thing)
{
return
string.Join("", thing.Children.Take(3).Select(x => x.TextData + ":")) +
string.Join("", thing.Children[3].Children.Select(x => x.TextData));
}
使用箭头符号:
static string GetServerInfo(Thing thing) =>
string.Join("", thing.Children.Take(3).Select(x => x.TextData + ":")) +
string.Join("", thing.Children[3].Children.Select(x => x.TextData));
等一下,现在我们有了一个 lambda。重写原始站点:
List<string> slowLog = reply.Children[0].Children
.Select(thing =>
string.Join("", thing.Children.Take(3).Select(x => x.TextData + ":")) +
string.Join("", thing.Children[3].Children.Select(x => x.TextData)))
.ToList();
我们只剩下一个声明了。 没有循环;没有循环问题。逻辑表达得非常清楚——取其中三个,将它们连接在一起,等等等等,把它变成一个列表,完成。
我们可以更进一步吗?当然!我们可以在一个帮助程序中组合选择和连接运算符,使我们的程序更流畅:
public static string SelectConcat<T>(this IEnumerable<T> items, Func<T, string> f) =>
string.Join("", items.Select(f));
现在我们的程序变成了
List<string> slowLog = reply.Children[0].Children
.Select(thing =>
thing.Children.Take(3).SelectConcat(x => x.TextData + ":") +
thing.Children[3].Children.SelectConcat(x => x.TextData))
.ToList();
现在非常清楚我们在做什么。
这里的要点是:目标不是让程序变小;这不是代码高尔夫。 让代码读起来更像其预期的语义。
您的原始代码使程序片段中最重要的事情看起来像是循环索引的值;查看有多少屏幕空间用于 i、j 和 k。这些是机制。编写代码以强调含义。如果你的意思是“拿前三个”,那么应该有一个 Take(3)
在某处,不是for
循环。
我们如何从强调 i、j 和 k 值的初始程序发展到描述数据操作的程序?看看我在那里做了什么:我做了一系列小而简单的重构,每一个都提高了抽象级别以使代码读起来更像它的预期语义或消除不必要的“仪式”。
关于c# - 比使用 3 个嵌套 for 循环获取我需要的数据更好的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45176782/
据我所知,根本不为元素呈现 HTML,或添加 display:none,似乎具有完全相同的行为:两者都使元素消失并且不与 HTML 交互。 我正在尝试禁用和隐藏一个复选框。所以HTML的总量很小;我无
我刚刚读了Android Architecture Tutorial: Developing an App with a Background Service (using IPC) .基本上是 让服
我有两个查询具有相同的结果,现在我想知道哪个查询更优化? 在选择中: select t1.*, sum(t2.value) as total_votes from table1 t1 left joi
有人告诉我,对于 I/O 绑定(bind)的应用程序,非阻塞 I/O 会更好。对于 CPU 密集型应用程序,阻塞 I/O 会好得多。我找不到这种说法的原因。试过谷歌,但很少有文章只是触及这个话题而没有
我有一个算法可以在数字列表中寻找好的对。一个好的配对被认为是索引 i 小于 j 且 arr[i] 1: # Finding the mid of the array
我有一个算法可以在数字列表中寻找好的对。一个好的配对被认为是索引 i 小于 j 且 arr[i] 1: # Finding the mid of the array
我从 API 收到一个 json,我需要解析并修改一个属性值。问题是,我收到的 json 数据的嵌套结构不一致,我无法控制它。 这将禁止我指定在特定深度(如 parsedJson.children[0
我有 451 个城市的坐标。现在我想计算每个城市之间的距离,然后根据该距离对一些结果进行排序。现在我有两个选择: 我可以运行一个循环来计算每个可能的城市组合的距离并将它们存储到一个表中,这将产生大约
对于返回相同结果的不同查询,我有两个查询计划我想知道是否有人可以告诉我哪个“更好”,以及为什么。 SELECT * FROM bids order by (select ranking from us
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 7 年前。 Improve this qu
我有一个二维数组。我需要尽可能快地对其执行一些操作(函数每秒将被调用十几次,所以让它变得高效会很好)。 现在,假设我想获取元素 A[i][j],简单地使用 A[i][j] 在速度上有什么不同吗和 *(
在声明或使用字符串的代码中,我通常会看到开发人员这样声明它: string randomString = @"C:\Random\RandomFolder\ThisFile.xml"; 代替: str
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Why don't CSS resets use '*' to cover all elements? 我正
如果我有一个包含许多重复项的 python 列表,并且我想遍历每个项目,而不是重复项,最好使用一个集合(如 set(mylist),或者找到另一种方法来创建没有重复的列表?我想只是循环遍历列表并检查重
在阅读常量接口(interface)反模式时,我发现没有实例的最终常量类比常量接口(interface)更好。 请解释一下怎么做? public interface ConstIfc { publ
我正在查看我继承的一些旧代码,我真的不喜欢某些地方的风格。我真的不喜欢它的外观的一件事是: bool func() { bool ret = true; ret &= test1();
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 4 年前。 Improv
我经常发现自己试图使用 boost/QT 信号解耦对象。实现这一点的简单方法是针对我要通信的每个具体类型,创建一个新的信号和插槽签名并连接所有相关对象。这导致了访问者模式,理想情况下我想发出一个访问者
我正在 https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html 上阅读有关 lambda 的内容 在方法
public List getInts() { List xs = new ArrayList(); xs.add(1); // return Collections.unmo
我是一名优秀的程序员,十分优秀!