HashMap
에 데이터를 저장하고 2 차원 문자열 배열로 변환 할 수있는 다음 클래스를 사용할 수 있습니다.
public class ArrayStructure {
private HashMap<Point, String> map = new HashMap<Point, String>();
private int maxRow = 0;
private int maxColumn = 0;
public ArrayStructure() {
}
public void add(int row, int column, String string) {
map.put(new Point(row, column), string);
maxRow = Math.max(row, maxRow);
maxColumn = Math.max(column, maxColumn);
}
public String[][] toArray() {
String[][] result = new String[maxRow + 1][maxColumn + 1];
for (int row = 0; row <= maxRow; ++row)
for (int column = 0; column <= maxColumn; ++column) {
Point p = new Point(row, column);
result[row][column] = map.containsKey(p) ? map.get(p) : "";
}
return result;
}
}
예제 코드
public static void main(String[] args) throws IOException {
ArrayStructure s = new ArrayStructure();
s.add(0, 0, "1");
s.add(1, 1, "4");
String[][] data = s.toArray();
for (int i = 0; i < data.length; ++i) {
for (int j = 0; j < data[i].length; ++j)
System.out.print(data[i][j] + " ");
System.out.println();
}
}
당신은 자바 컬렉션 프레임 워크를 생각 해 봤나
1
4
출력? – Smutje