我的函数应该用"&"分割字符串。 &#34 ;;"和","并返回三个锯齿状阵列,反之亦然。 (来自如下数据:1,2,3; 4,5,6和1,2,3; 4,5,6)
我一直在努力使其发挥作用,现在出于某种原因我在
上获得了system.argumentnullexceptionArray.Copy(playerOneEnts, allEnts[0], playerOneEnts.Length);
Array.Copy(playerTwoEnts, allEnts[1], playerTwoEnts.Length);
完整代码:
public string convertToString(string[][][] allEnts)
{
string Player = string.Empty;
string[][] playerOneEnts = new string[maxEnts][];
string[][] playerTwoEnts = new string[maxEnts][];
Array.Copy(allEnts[0], playerOneEnts, allEnts[0].Length);
Array.Copy(allEnts[1], playerTwoEnts, allEnts[1].Length);
for (int j = 0; j < playerOneEnts.Length; j++)
{
for (int i = 0; i < playerOneEnts[j].Length; i++)
{
Player += playerOneEnts[j][i] + ",";
}
Player = Player.TrimEnd(',');
Player += ";";
}
Player = Player.TrimEnd(';');
Player += "&";
for (int j = 0; j < playerTwoEnts.Length; j++)
{
for (int i = 0; i < playerTwoEnts[j].Length; i++)
{
Player += playerTwoEnts[j][i] + ",";
}
Player = Player.TrimEnd(',');
Player += ";";
}
Player = Player.TrimEnd(';');
return Player;
}
public string[][][] convertToArray(string ents)
{
string[] p = new string[2];
string[][] playerOneEnts = new string[maxEnts][];
string[][] playerTwoEnts = new string[maxEnts][];
string[][][] allEnts = new string[2][][];
p = ents.Split('&');
try
{
playerOneEnts = p[0].Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries).Select(chunk => chunk.Split(',')).ToArray();
playerTwoEnts = p[1].Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries).Select(chunk => chunk.Split(',')).ToArray();
MessageBox.Show(playerOneEnts.Length.ToString());
Array.Copy(playerOneEnts, allEnts[0], playerOneEnts.Length);
Array.Copy(playerTwoEnts, allEnts[1], playerTwoEnts.Length);
}
catch
{
MessageBox.Show("unable to convert string", "Fatal Error");
}
return allEnts;
}
对我来说,代码看起来像是一场灾难,如果有人知道将它转换为字符串的更好方法,我会为任何想法感到高兴。摆脱我的错误已经足够帮助我了。
谢谢!
答案 0 :(得分:5)
以下是使用Linq的示例(需要.Net 4.0):
var source = "1,2,3;4,5,6&1,2,3;4,5,6";
string[][][] decoded = source.Split('&').Select(x => x.Split(';').Select(y => y.Split(',').ToArray()).ToArray()).ToArray();
string reencoded = String.Join("&", decoded.Select(x => String.Join(";", x.Select(y => String.Join(",", y)))));
或者,IEnumarable泛型可用于避免转换为数组。
警告:此代码不会验证输入,例如数组长度。
编辑:.NET 3.5的重编码器:
var reencoded = String.Join("&", decoded.Select(x => String.Join(";", x.Select(y => String.Join(",", y)).ToArray())).ToArray());
答案 1 :(得分:1)
您可以使用嵌套的for循环来执行此操作:
public synchronized void start(int max) {
try {
// max = total number of questions
for (int i = 1; i < max; i++) {
//new cmd command
Process p = Runtime.getRuntime().exec("cmd");
// new thread
new Thread(() -> {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null)
System.out.println(line);
}
catch (Exception e) {
e.printStackTrace();
}
}).start();
//used to change text in files and the remaining cmd commands and move the result
try (PrintStream out = new PrintStream(p.getOutputStream())) {
//to clear previous index folder and delete results folder
deleteFile();
//to rewrite the "collection.spec" folder with the address of the Answers file
answerFile(i);
//to rewrite the "trec.topics.list"" folder with the address of the Questions file
questionFile(i);
/* PROBLEM STARTS HERE! */
for(int c = 1; c <= 17; c++) {
// after rename do the remaining cmd commands
out.println("C:/TERRIERS/terrier/bin/trec_terrier.bat -i");
out.println("C:/TERRIERS/terrier/bin/trec_terrier.bat -r");
//change model
modelSelect();
}
/* PROBLEM ENDS HERE!*/
// .....
}
p.waitFor();
//move the ".res" folder to the Results folder in projects directory
takeFile(i);
}
}
catch(IOException ex) {
}
catch(InterruptedException ex) {
}
}
编码类似:
int counter = 1;
public void modelSelect() {
//location of file
String s = "C:\\TERRIERS\\terrier\\etc\\trec.models";
try {
FileOutputStream is = new FileOutputStream(s);
OutputStreamWriter osw = new OutputStreamWriter(is);
Writer w = new BufferedWriter(osw);
//depends on count value change the model inside "trec.models" and increase counter by 1
switch (counter) {
case 1 : w.write("uk.ac.gla.terrier.matching.models.BM25"); counter++; break;
case 2 : w.write("uk.ac.gla.terrier.matching.models.DFR_BM25"); counter++; break;
case 3 : w.write("uk.ac.gla.terrier.matching.models.TF_IDF"); counter++; break;
case 4 : w.write("uk.ac.gla.terrier.matching.models.BB2"); counter++; break;
case 5 : w.write("uk.ac.gla.terrier.matching.models.IFB2"); counter++; break;
case 6 : w.write("uk.ac.gla.terrier.matching.models.In_expB2"); counter++; break;
case 7 : w.write("uk.ac.gla.terrier.matching.models.In_expC2"); counter++; break;
case 8 : w.write("uk.ac.gla.terrier.matching.models.InL2"); counter++; break;
case 9 : w.write("uk.ac.gla.terrier.matching.models.PL2"); counter++; break;
case 10 : w.write("uk.ac.gla.terrier.matching.models.DFR"); counter++; break;
case 11 : w.write("uk.ac.gla.terrier.matching.models.DFRWeightingModel"); counter++; break;
case 12 : w.write("uk.ac.gla.terrier.matching.models.DLH"); counter++; break;
case 13 : w.write("uk.ac.gla.terrier.matching.models.DLH13"); counter++; break;
case 14 : w.write("uk.ac.gla.terrier.matching.models.Hiemstra_LM"); counter++; break;
case 15 : w.write("uk.ac.gla.terrier.matching.models.Idf"); counter++; break;
case 16 : w.write("uk.ac.gla.terrier.matching.models.LemurTF_IDF"); counter++; break;
case 17 : w.write("uk.ac.gla.terrier.matching.models.WeightingModel"); counter=1; break;//reset counter to 1
}
w.close();
}
catch (IOException e) {
}
}