日期:2014-05-17  浏览次数:20722 次

写一个java程序,读取map01.txt文档中坐标数据(x、y、z轴),写入如下map图中
map01.txt 中数据(就是一个三维数组,如第一行数据,x=1,y=6,要录入的数据是z=1,后面0不要管)

  
1 6 1 0
1 7 1 0
2 6 1 0
2 7 1 0
3 6 1 0
3 7 1 0


写一个java程序,得到如下图:
  010203040506070809101112
001||
002||
003||
004||
005||
006||010101
007||010101
008||

------解决方案--------------------
好像明白LZ的意思了,把map信息转成一个2维数组

Java code
import java.util.*;
public class Test {
    public static void main(String[] args) throws Throwable {
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("map01.txt")));
        String buf = "";
        List<List<Integer>> list = new ArrayList<List<Integer>>();
        while ((buf=br.readLine()) != null) {
            if (! buf.matches("(\\d+(\\s+|$)){3,}")) continue;
            String[] sa = buf.split("\\s+");
            int x = Integer.valueOf(sa[0]);
            int y = Integer.valueOf(sa[1]);
            int z = Integer.valueOf(sa[2]);
            while (list.size() < y) {
                list.add(new ArrayList<Integer>());
            }
            List<Integer> sub = list.get(y-1);
            while (sub.size() < x) {
                sub.add(0);
            }
            sub.set(x-1, z);
        }

        System.out.printf("     ");
        for (int i=0; i<12; i++) {
            System.out.printf("%02d ", i+1);
        }
        System.out.println();

        for (int i=0; i<8; i++) {
            System.out.printf("%03d||", i+1);
            if (i<list.size()) {
                List<Integer> sub = list.get(i);
                for (int j : sub) {
                    if (j == 0) {
                        System.out.printf("   ");
                    } else {
                        System.out.printf("%02d ", j);
                    }
                }
            }
            System.out.println();
        }
    }
}