【问题标题】:How can I store data from a while-loop in an array?如何将来自 while 循环的数据存储在数组中?
【发布时间】:2019-10-22 08:34:22
【问题描述】:

我需要存储我通过扫描仪从带有 while 循环的 .txt 文件中收集的数据。 .txt 中的行包含 x、y、z 坐标和 c 值。 我设法从 .txt 中拆分行并将它们转换为数字,但我无法在循环之外访问它们以从那里使用它们。我想在二维数组“z65”中使用“x-”和“y-”坐标和 c 值。

import java.io.File;  
import java.util.Arrays;  
import java.util.Scanner;  
import java.util.regex.Pattern;  
import gdi.ct.CtImage ;

public class CtViewer {  
 public static void main(String[] args)   
   throws java.io.FileNotFoundException    
 {

  int x;  
  int y;  
  int z;  
  int c;  
  int xValue;  
  int yValue;  
  int zValue;  
  int cValue;

  int [] [] [] ct = new int [204] [204] [139];

  for ( x = 0; x < 204; x++) {  
   for ( y = 0; y < 204; y++) {  
    for ( z = 0; z < 139; z++) {  
     ct[x][y][z] =  -1000; }}}  
  System.out.println(ct [0] [0] [0]);  
  System.out.println(ct [0] [0] [1]);

  File cTxt = new File("CT-Chest.txt");  
  Scanner readTxt = new Scanner(cTxt);

  while(readTxt.hasNextLine()) {  
   String i = readTxt.nextLine();

   String [] out = i.split(" ");

   xValue = Integer.parseInt(out[0]);  
   yValue =Integer.parseInt(out[1]);  
   zValue= Integer.parseInt(out[2]);  
   cValue =Integer.parseInt(out[3]);


   x =xValue;  
   y=yValue;  
   z=zValue;  
   c = cValue;  
   System.out.println(x+ "" +y+"" +z+ ""+c);    //Works 'till     here.  
   ct [x] [y][z] = c;


  }  
   System.out.println(x+""+y);  
  int[] [] z65 = new int [x] [y];  
  CtImage cI = new CtImage(z65);







 }  
 }  

【问题讨论】:

  • xyz 有哪些值?它们应该在0 &lt;= x &lt; 2040 &lt;= y &lt; 2040 &lt;= x &lt; 139 之间才能做到ct [x] [y][z] = c;
  • 正确。这些值如您所描述。

标签: java arrays multidimensional-array while-loop


【解决方案1】:

你写完了你的循环变量。变成这样

  x =xValue;  //delete all this
 y=yValue;  
 z=zValue;  
 c = cValue;/// to here
System.out.println(x+ "" +y+"" +z+ ""+c);    //Works 'till     here.  
ct [xValue] [yValue][zValue] = cValue;  

这就是我能够做到的:

int[][][] ct = new int[204][204][139];
    for (int x = 0; x < 204; x++) {
        for (int y = 0; y < 204; y++) {
            for (int z = 0; z < 139; z++) {
                ct[x][y][z] = -1000;
            }
        }
    }
    File file = new File(ClassLoader.getSystemResource("CT-Chest.txt").getFile());
    List<String> lines = Files.readAllLines(file.toPath());
    lines.stream().filter(s->!s.trim().isEmpty()).forEach(s->{
        String[] split = s.split(" ");
        ct[Integer.parseInt(split[0])][Integer.parseInt(split[1])][Integer.parseInt(split[2])]=Integer.parseInt(split[3]);
    });
    System.out.println(ct[5][8][99]);

以及我创建的文件的内容:

5 8 99 2
6 203 138 2000
203 1 88 135

程序的输出是2。

您是否有可能尝试在其他方法中使用? ct 并且从该文件中读取的所有值都将从那时起可用。我从文件中得到的行有点不同,但这没关系。

【讨论】:

  • 其实它在循环之外,所以那些不应该存在
  • 它仍然不允许我从循环外部访问值。
  • 它告诉我底部 z65 数组的变量 X 和 y 未初始化
  • 我刚刚注意到的 1 个问题。 int[] [] z65 = new int [x] [y]; CtImage cI = new CtImage(z65); 不使用我们刚刚放置所有数据的 ct 数组 为什么?您提供的代码甚至没有使用数组。是创建 DImention X、Y 的图像和 Z=1 的 C 值的想法吗?你什么都不做。您确实尝试创建一个大小为 X,Y 的 int 数组,但您没有在其中放入任何数据。
  • 我稍微修改了代码,希望它能够正常工作:
【解决方案2】:

如果将来有人需要针对这个确切问题的解决方案,这就是我的解决方案。

        import java.io.File;
        import java.util.Arrays;
        import java.util.Scanner;
        import java.util.regex.Pattern;
        import gdi.ct.CtImage;

        public class CtViewer {
            public static void main(String[] args) throws java.io.FileNotFoundException {

                int x;
                int y;
                int z;
                int c;
                int[][][] ct = new int[204][204][139];

                for (x = 0; x < 204; x++) {
                    for (y = 0; y < 204; y++) {
                        for (z = 0; z < 139; z++) {
                            ct[x][y][z] = -1000;
                        }
                    }
                }

                File cTxt = new File("CT-Chest.txt");
                Scanner readTxt = new Scanner(cTxt);
                int[][] z65 = new int[204][204];
                do {
                    String i = readTxt.nextLine();
                    String[] out = i.split(" ");

                    int xValue = Integer.parseInt(out[0]);
                    int yValue = Integer.parseInt(out[1]);
                    int zValue = Integer.parseInt(out[2]);
                    ct[xValue][yValue][zValue] = Integer.parseInt(out[3]);
                    if (zValue == 65) {
                        xValue = Integer.parseInt(out[0]);
                        yValue = Integer.parseInt(out[1]);
                        zValue = Integer.parseInt(out[2]);
                        z65[xValue][yValue] = Integer.parseInt(out[3]);
                    }
                }
                while (readTxt.hasNextLine());
                CtImage cI = new CtImage(z65);


                readTxt.close();
            }


        }

【讨论】:

    猜你喜欢
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    • 2011-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多