java - 用JAVA填充二维数组
问题描述
我有一个文件,其中包含必须插入到二维数组中的数字,但它在这里不起作用是我的代码抱歉英语不好
文件
2,1 //starting point
3,2 //ending point
5,6 //array size which is maze
0,1,1,1,1,1 //from here till end its all gonna be inserted to 2d array
0,1,0,0,1,1 //and i somehow managed to take starting, ending point and arraysize
0,1,0,0,1,0 //all i have to do is fill the array
0,0,1,0,1,0
0,0,1,1,1,0
完整代码
public class Mouse {
// global variables to take input from file
public static int startRow;
public static int startCol;
public static int endRow;
public static int endCol;
public static int arrayRow;
public static int arrayCol;
public static String arrayDetail;
public static void main(String[] args) throws IOException {
StringBuilder stringbuilder = new StringBuilder();
try {
BufferedReader input = new java.io.BufferedReader(new java.io.FileReader("src/input.txt"));
List<String> lines = new ArrayList<String>();
String[] stringArray = new String[lines.size()];
String line = null;
while ((line = input.readLine()) != null) {
lines = Arrays.asList(line.split("\\s*,\\s*"));
stringArray = lines.toArray(stringArray);
for (int i = 0; i < stringArray.length; i++) {
stringbuilder.append(stringArray[i]);
}
}
}
catch (FileNotFoundException e) {
System.err.println("File not found.");
}
System.out.println(stringbuilder);
//giving global variables a value that i need
startRow = Integer.parseInt(stringbuilder.substring(0, 1));
startCol = Integer.parseInt(stringbuilder.substring(1, 2));
endRow = Integer.parseInt(stringbuilder.substring(2, 3));
endCol = Integer.parseInt(stringbuilder.substring(3, 4));
arrayRow = Integer.parseInt(stringbuilder.substring(4, 5));
arrayCol = Integer.parseInt(stringbuilder.substring(5, 6));
arrayDetail = stringbuilder.substring(6);
//i cant give array values to the long variable because it's out of range
Home home = new Home();
home.print(); // print maze before releasing mouse
if (home.walk(startRow,startCol)) { //starting position
System.out.println("Ok"); //print okay if its successful
}
else {
System.out.println("No Solution"); //if unsuccessful
}
home.print(); // print maze to track how mouse went
}
}
class Home{
Home(){}
int[][] A = new int[Mouse.arrayRow][Mouse.arrayCol]; //giving array size
{
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < A[i].length; j++) {
for (int k = 0; k < Mouse.arrayDetail.length(); k++) {
//and its the problem doesn't take values
A[i][j] = Integer.parseInt(Mouse.arrayDetail.substring(k, k+1));
//and it gives me bunch of 000 it means its null and not taking values right ?
}
}
}
}
// road is 1
// wall is 0
// the roads that mouse went only one time is 2
// 3 is the road that mouse went but did not get success i mean the roads mouse went 2 times
public boolean walk(int row, int col) { //method to walk
boolean result = false;
if (check(row, col)){
A[row][col] = 3;
if (row == Mouse.endRow && col == Mouse.endCol) { //ending position
result = true;
}
else {
result = walk(row+1, col); //down
if(!result)
result = walk(row, col+1); //right
if(!result)
result = walk(row-1, col); //up
if(!result)
result = walk(row, col-1); //left
}
}
if (result == true) {
A[row][col] = 2;
}
return result;
}
public boolean check(int row, int col) { //check there is a road
boolean result = false;
if (row<A.length && row >=0 && col >=0 && col < A[0].length ) {
if (A[row][col] == 1) {
result = true;
}
}
return result;
}
public void print() { //method to print maze
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < A[i].length; j++) {
System.out.print(A[i][j]);
}
System.out.println();
}
}
}
输出
000000
000000
000000
000000
000000
No Solution
000000
000000
000000
000000
000000
// 看起来你的帖子主要是代码;请添加更多细节。所以我添加了无意义的文本。看起来您的帖子主要是代码;请添加更多细节。所以我添加了无意义的文本。
解决方案
您可以像这样填充二维数组 A
int[][] A = new int[Mouse.arrayRow][Mouse.arrayCol]; //giving array size
{
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < A[i].length; j++) {
A[i][j] = Mouse.arrayDetail.charAt(i * Mouse.arrayCol + j) - '0';
}
}
}
推荐阅读
- python-3.x - 如何为 unix 打包/冻结 python 代码?
- c# - 无法使用 XDocument 从 XML 中提取子元素
- r - 使用分组和汇总向数据框添加新行
- c++ - 用天数推算出准确的月份
- r - 一个滑块控制 R 中的多个子图
- python - 在 python 循环期间暂停的最有效方法是什么?
- f# - 定义中有意外的标识符。预期在此点或之前的不完整结构化构造或其他标记
- c++ - std::deque 和 std::vector 范围构造函数之间的差异
- r - TidyR 分离:最后出现的下划线
- javascript - 我可以在我的网站上嵌入谷歌日历,以便用户可以登录/查看自己的日历