如何将while循环中的数据存储在数组中?

时间:2019-06-06 19:39:19

标签: java arrays multidimensional-array while-loop

我需要存储通过While循环从.txt文件通过Scanner收集的数据。 .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);







 }  
 }  

2 个答案:

答案 0 :(得分:0)

您正在写循环变量。像这样

  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
  6203138 2000
  203 1 88135

程序的输出为2。

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

答案 1 :(得分:0)

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

        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();
            }


        }