Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.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 以特定格式打印二维数组的ArrayList_Java - Fatal编程技术网

Java 以特定格式打印二维数组的ArrayList

Java 以特定格式打印二维数组的ArrayList,java,Java,我正在尝试以特定格式将ArrayList保存到文本文件中。它的格式正确,但它只打印出块的ArrayList中一个元素的颜色。我知道问题在于getBlockColor()方法,实现此方法的最佳方法是什么?这是我到目前为止得到的 这是类中具有帧ArrayList的方法 public void saveFrames(String fileName) { System.out.println("**method save writes data back to a file "

我正在尝试以特定格式将ArrayList保存到文本文件中。它的格式正确,但它只打印出块的ArrayList中一个元素的颜色。我知道问题在于getBlockColor()方法,实现此方法的最佳方法是什么?这是我到目前为止得到的

这是类中具有帧ArrayList的方法

public void saveFrames(String fileName) {
    System.out.println("**method save writes data back to a file "
            + fileName);
    try {
        PrintWriter outfile = new PrintWriter(new OutputStreamWriter(
                new FileOutputStream(fileName)));
        outfile.println(frames.size());
        outfile.println(Frame.getCOLUMNS());
        for (Frame f : frames) {
            for (int i = 0; i < 20; i++) {
                for (int j = 0; j < 20; j++) {
                    Color a = f.getBlockColor();
                    if (a.equals(Color.white)) {
                        outfile.print("w");
                    }
                    if (a.equals(Color.orange)) {
                        outfile.print("o");
                    }
                    if (a.equals(Color.red)) {
                        outfile.print("r");
                    }
                    if (a.equals(Color.yellow)) {
                        outfile.print("y");
                    }
                    if (a.equals(Color.green)) {
                        outfile.print("g");
                    }
                    if (a.equals(Color.blue)) {
                        outfile.print("b");
                    }

                }
                outfile.println("");
            }
        }
        outfile.close();
    }

    catch (IOException e) {
        System.out.println("file not found try again");
    }
}
public void存储框架(字符串文件名){
System.out.println(“**方法save将数据写回文件”
+文件名);
试一试{
PrintWriter outfile=新的PrintWriter(新的OutputStreamWriter(
新文件输出流(文件名));
outfile.println(frames.size());
outfile.println(Frame.getCOLUMNS());
用于(帧f:帧){
对于(int i=0;i<20;i++){
对于(int j=0;j<20;j++){
颜色a=f.getBlockColor();
如果(a.equals(Color.white)){
输出文件打印(“w”);
}
如果(a.equals(Color.orange)){
输出文件。打印(“o”);
}
如果(a等于(红色)){
输出文件打印(“r”);
}
如果(a等于(黄色)){
输出文件打印(“y”);
}
如果(a.equals(Color.green)){
输出文件打印(“g”);
}
如果(a等于(蓝色)){
输出文件打印(“b”);
}
}
outfile.println(“”);
}
}
outfile.close();
}
捕获(IOE异常){
System.out.println(“未找到文件,请重试”);
}
}
这是来自帧的代码,应该获得块的颜色

public Color getBlockColor() {
    for (int ROWS = 0; ROWS < 20; ROWS++) {
        for (int COLUMNS = 0; COLUMNS < 20; COLUMNS++) {
            blockColor = blocks[ROWS][COLUMNS].getBackground();
        }
    }
    return blockColor;
}
public Color getBlockColor(){
用于(int行=0;行<20;行++){
for(int COLUMNS=0;COLUMNS<20;COLUMNS++){
blockColor=块[行][列].getBackground();
}
}
返回块颜色;
}

我认为您在获取块颜色时犯了一个小错误。 我猜您应该返回特定行和列的颜色,如下所示:

public Color getBlockColor(int row, int column) {
    return blocks[row][column].getBackground();
}
getBlockColor()
始终返回
块[19][19]。getBackground()
。这是你想要的吗?