我有这个字符串:
text = "book//title//page/section/para";
Run Code Online (Sandbox Code Playgroud)
我想通过它来查找所有//和/和它们的索引.
我尝试这样做:
if (text.Contains("//"))
{
Console.WriteLine(" // index: {0} ", text.IndexOf("//"));
}
if (text.Contains("/"))
{
Console.WriteLine("/ index: {0} :", text.IndexOf("/"));
}
Run Code Online (Sandbox Code Playgroud)
我也在考虑使用:
Foreach(char c in text)
Run Code Online (Sandbox Code Playgroud)
但它不起作用,因为//不是一个单一的char.
我怎样才能实现我的目标?
我也尝试了这个,但没有显示结果
string input = "book//title//page/section/para";
string pattern = @"\/\//";
Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
MatchCollection matches = rgx.Matches(input);
if (matches.Count > 0)
{
Console.WriteLine("{0} ({1} matches):", input, matches.Count);
foreach (Match match in matches)
Console.WriteLine(" " + input.IndexOf(match.Value));
}
Run Code Online (Sandbox Code Playgroud)
先感谢您.
我遍历KeyValuePair的集合,然后将Key和Value复制到一个新创建的类中,如下所示:
Classes.MemberHierarchy membHier = new Classes.MemberHierarchy();
List<Classes.MemberHierarchy> membHierList = new List<Classes.MemberHierarchy>();
foreach (KeyValuePair<string, string[]> acct in acctData)
{
membHier.entityName = acct.Key;
membHier.Accounts = acct.Value;
membHierList.Add(membHier);
}
Run Code Online (Sandbox Code Playgroud)
问题在于,在第二次迭代中,membHierList属性立即被第一次迭代中的值覆盖。真奇怪
因此,在第一次迭代时,membHier.entityName是“ ABC Member”,并且使用字符串数组填充Accounts没问题。
然后在第二次迭代中,membHier.entityName为“ XYZ成员”。
现在,“ XYZ成员”占据了两个插槽,如下所示
membHierList [0] .base.entityName =“ XYZ成员” membHierList [1] .base.entityName =“ XYZ成员”
我上方有物体冲突吗?
预先谢谢你。
我正在计算操作期间的许多(~1亿)浮点值.我不想将它们全部存储在内存中,但我想保存集合的粗略分布.
我的想法是确定所有值的指数并在直方图中计算它们.但是,这当然只有在值具有不同的指数时才有效.
有谁知道如何在不知道分布如何的情况下做到这一点?
我一直在使用googlemaps,我现在正在寻找格式坐标.
我得到以下格式的坐标:
地址(坐标)缩放级别.
我使用indexof方法得到"("+1的开头,这样我得到坐标的第一个数字,并将这个值存储在我称之为"start"的变量中.
然后我做同样的事情,但这次我得到索引")"-2得到最后一个坐标的最后一个数字,并将这个值存储在我称之为"结束"的变量中.
我收到以下错误:"索引和长度必须引用string.Parameter name中的位置:length"
我得到以下字符串作为一个imparameter:
"Loddvägen 155, 840 80 Lillhärdal, Sverige (61.9593214318303,14.0585965625)5"
Run Code Online (Sandbox Code Playgroud)
根据我的计算,我应该在start变量中得到值36,在end变量中得到值65
但出于某种原因,我在开始时得到41,在结束时得到71.
为什么?
public string RemoveParantheses(string coord)
{
int start = coord.IndexOf("(")+1;
int end = coord.IndexOf(")")-2;
string formated = coord.Substring(start,end);
return formated;
}
Run Code Online (Sandbox Code Playgroud)
然后我尝试硬编码正确的值
string Test = cord.Substring(36,65);
Run Code Online (Sandbox Code Playgroud)
然后我得到以下错误:
startindex不能大于字符串的长度.参数名称startindex
我理解这两个错误的意思,但在这种情况下,它们是不正确的,因为我不会超出字符串长度值.
谢谢!
这是我的代码:
Parallel.ForEach(Students, item =>
{
StudentModel studentModel = new StudentModel(item);
// Maybe he/she has alot of name
foreach (var words in studentModel.StudentNames.Split(','))
{
if (string.IsNullOrWhiteSpace(words))
return;
string tempWords = words.Trim();
// add it to student names list
STUDENT_NAMES.Where(x => x.SearchWords == tempWords).FirstOrDefault().student.Add(studentModel);
}
// add it to student list
STUDENT_MODELS.Add(studentModel);
});
Run Code Online (Sandbox Code Playgroud)
我想做的是,我得到了学生名单.将其转换为学生模型,获取学生姓名(因为一个学生有很多名字),然后我将名字添加到名单,这是因为可能以后我需要得到同名学生并做一些事情......最后添加学生到学生模特名单.
问题发生在:
STUDENT_NAMES.Where(x => x.SearchWords == tempWords).FirstOrDefault().student.Add(studentModel);
Run Code Online (Sandbox Code Playgroud)
这个地方总是发生:System.IndexOutOfRangeException
我已经将Paralle.Foreach更改为Parallel.For,并将foreach更改为for,但没有任何更改.我必须使用Parallel,因为学生数量大约为100000,如果我只使用foreach替换Parallel.Foreach,则需要160+秒,如果我锁定那个地方.....还是慢......如果使用Parallel. Foreach,它将使用20秒左右,但我无法处理异常.
我已经尝试用这个替换它:
StudentNames name = STUDENT_NAMES.Where(x => x.SearchWords == tempWords).FirstOrDefault();
if (null != name)
name.student.Add(StudentModel);
Run Code Online (Sandbox Code Playgroud)
但问题仍然发生在某些时候........如果我只是尝试...抓住并忽略它,然后当我以后访问STUDENT_NAMES列表时,它仍然抛出异常....... .....
我也尝试使用ConcurrentBag …
我在面试时被问到这个问题.我认为这个问题过于通用,无法指定特定的数据结构.
但是,如果我们将问题通道化为以下标准,那么使用的最佳数据结构是什么:
我想在堆的插入函数中使用 C 中的 realloc。这是代码:
\ntypedef struct MaxHeap {\n int size;\n int* heap;\n} MaxHeap;\n\nvoid max_insert(MaxHeap* max_heap, int* key_index, int key) { // O(logn)\n max_heap->heap = realloc((max_heap->size+1), sizeof * max_heap->heap);\n max_heap[max_heap->size] = N_INF;\n max_heap->size += 1;\n increase_key(max_heap, key_index, max_heap->size, key)\n}\nRun Code Online (Sandbox Code Playgroud)\n我收到这个警告:
\nwarning: passing argument 1 of \xe2\x80\x98realloc\xe2\x80\x99 makes pointer from integer without a cast [-Wint-conversion]我尝试了这个修复:
max_heap->heap = realloc((max_heap->heap), (max_heap->size+1) * sizeof(*(max_heap->heap)));\n\nRun Code Online (Sandbox Code Playgroud)\n更新
\n我这样做了:
\nvoid max_insert(MaxHeap* max_heap, int* key_index, int key) { // O(logn)\n int* …Run Code Online (Sandbox Code Playgroud) 静态类与仅使用静态方法(例如私有构造函数)的类有什么区别?
说我们有:
using System;
public class Test
{
public static void Main()
{
Info.SetName("nnn");
Console.WriteLine(Info.Name);
Info.SetName("nn2");
Console.WriteLine(Info.Name);
Info2.SetName("nnn");
Console.WriteLine(Info2.Name);
Info2.SetName("nn2");
Console.WriteLine(Info2.Name);
}
}
public class Info
{
public static string Name;
public static void SetName(string name){
Name = name;
}
}
public static class Info2
{
public static string Name;
public static void SetName(string name){
Name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
那么从方法/属性访问性能,代码可读性,代码可扩展性的角度来看?