Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为行问题拆分_Java_Templates_Split - Fatal编程技术网

Java 为行问题拆分

Java 为行问题拆分,java,templates,split,Java,Templates,Split,我有一个密码: public static CuttingTemplate parseCuttingTemplate(String[] lines) { int size = lines.length; int[] arr = new int[size]; int i; for(i = 0; i < size; ++i) { if (i == 0) { arr[i]

我有一个密码:

public static CuttingTemplate parseCuttingTemplate(String[] lines) {


        int size = lines.length;
        int[] arr = new int[size];

        int i;
        for(i = 0; i < size; ++i) {
            if (i == 0) {
                arr[i] = Integer.parseInt(lines[i]);
            }

            if (i >= 1) {
                arr[i] = Integer.parseInt(lines[i]);
            }
        }

        int width = arr[0];
        int height = arr[0];
        CuttingTemplate ct = new CuttingTemplate(width, height);
        return ct;
    }
publicstaticcuttingTemplateparsecuttingTemplate(字符串[]行){
int size=lines.length;
int[]arr=新的int[size];
int i;
对于(i=0;i=1){
arr[i]=Integer.parseInt(行[i]);
}
}
int width=arr[0];
内部高度=arr[0];
切割模板ct=新的切割模板(宽度、高度);
返回ct;
}

我需要找到一种方法将
.split(;”)和
.split(->)添加到我的行中。txt文件已准备好读取,现在我只需向其中添加一条“规则”,但我不确定在哪里…

您想按
拆分文本
->
?此外,循环中的if语句是不必要的,因为它们都执行相同的操作,因此
arr[i]=integer…
本身应该执行相同的操作
->
?此外,循环中的if语句是不必要的,因为它们都执行相同的操作,因此
arr[i]=integer…
本身应该是相同的

我不确定我是否完全理解这个问题,但基于您相同的
arr[i]=integer.parseInt(行[i])行,我假设您要在
上拆分,在另一种情况下为
->
拆分。您的
行[i]
是您正在循环的单个
字符串
行,因此您可以执行以下操作:

if (i == 0) {
  String[] splitBySemicolon = lines[i].split(";");
  // Get the integer before the ';':
  arr[i] = Integer.parseInt(splitBySemicolon[0]);
  // or alternatively:
  // Get the integer after the ';':
  arr[i] = Integer.parseInt(splitBySemicolon[1]);
}
// Minor note: you loop in the range [0, size),
// so you can just use an else here since i cannot be negative
else{
  String[] splitByArrow = lines[i].split("->");
  // Get the integer before the '->':
  arr[i] = Integer.parseInt(splitByArrow[0]);
  // or alternatively:
  // Get the integer after the '->':
  arr[i] = Integer.parseInt(splitByArrow[1]);
}
例如,假设您的lines数组包含
[“1;header”,“3->something”,“4->somethingMore”]
,上面的代码(使用
[0]
对字符串数组进行索引)将导致
arr=[1,3,4]


但是在不知道实际的
行数的情况下,我只能猜测您的预期行为。

我不确定我是否完全理解这个问题,但基于您相同的
arr[I]=Integer.parseInt(行数[I])行,我假设您要在
上拆分,在另一种情况下为
->
拆分。您的
行[i]
是您正在循环的单个
字符串
行,因此您可以执行以下操作:

if (i == 0) {
  String[] splitBySemicolon = lines[i].split(";");
  // Get the integer before the ';':
  arr[i] = Integer.parseInt(splitBySemicolon[0]);
  // or alternatively:
  // Get the integer after the ';':
  arr[i] = Integer.parseInt(splitBySemicolon[1]);
}
// Minor note: you loop in the range [0, size),
// so you can just use an else here since i cannot be negative
else{
  String[] splitByArrow = lines[i].split("->");
  // Get the integer before the '->':
  arr[i] = Integer.parseInt(splitByArrow[0]);
  // or alternatively:
  // Get the integer after the '->':
  arr[i] = Integer.parseInt(splitByArrow[1]);
}
例如,假设您的lines数组包含
[“1;header”,“3->something”,“4->somethingMore”]
,上面的代码(使用
[0]
对字符串数组进行索引)将导致
arr=[1,3,4]


但是,在不知道实际的
数组的情况下,我只能猜测您的预期行为。

准确无误。我的模板是这样的300;300 20;20->20;80 20;80->80;80 80;80->80;20 80;20->20;20 100;100->100;266 100;266->266;266 266;266->266;100 266;100->100;100 0;0->300;例如,从第1行到第10行的300。我的模板是这样的300;300 20;20->20;80 20;80->80;80 80;80->80;20 80;20->20;20 100;100->100;266 100;266->266;266 266;266->266;100 266;100->100;100 0;0->300;例如,从1号线到10号线的300