java 动态读写二维数组
时间: 2023-10-03 10:02:44 浏览: 263
Java中的二维数组可以用以下方式动态读写:
1. 初始化二维数组
int[][] array = new int[row][column];
其中,row是行数,column是列数。
2. 读取二维数组中的值
int value = array[rowIndex][columnIndex];
其中,rowIndex是行索引,columnIndex是列索引。
3. 修改二维数组中的值
array[rowIndex][columnIndex] = newValue;
其中,rowIndex是行索引,columnIndex是列索引,newValue是要修改的新值。
示例代码:
```
int[][] array = new int[2][3]; // 初始化二维数组
// 读取二维数组中的值
int value = array[1][2];
// 修改二维数组中的值
array[1][2] = 10;
```
相关问题
java 动态二维数组文件读写
1. 文件读取
```java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Dynamic2DArrayFileReader {
public static void main(String[] args) {
try {
File file = new File("input.txt");
Scanner scanner = new Scanner(file);
// Read the number of rows and columns
int rows = scanner.nextInt();
int cols = scanner.nextInt();
// Initialize the 2D array
int[][] array = new int[rows][cols];
// Read the elements of the array
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
array[i][j] = scanner.nextInt();
}
}
// Print the array
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
```
2. 文件写入
```java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class Dynamic2DArrayFileWriter {
public static void main(String[] args) {
int[][] array = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
try {
File file = new File("output.txt");
PrintWriter writer = new PrintWriter(file);
// Write the number of rows and columns
writer.println(array.length);
writer.println(array[0].length);
// Write the elements of the array
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[0].length; j++) {
writer.print(array[i][j] + " ");
}
writer.println();
}
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
```
java 二维数组文件读写
Java 二维数组可以通过文件读写来进行操作。
文件读取:
```java
import java.io.*;
public class ReadFile {
public static void main(String[] args) {
try {
File file = new File("filename.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
int[][] arr = new int[3][3];
int i = 0;
while ((line = br.readLine()) != null) {
String[] values = line.split(",");
for (int j = 0; j < values.length; j++) {
arr[i][j] = Integer.parseInt(values[j]);
}
i++;
}
br.close();
for (int[] row : arr) {
for (int value : row) {
System.out.print(value + " ");
}
System.out.println();
}
} catch (IOException e) {
System.out.println("Error reading file");
e.printStackTrace();
}
}
}
```
文件写入:
```java
import java.io.*;
public class WriteFile {
public static void main(String[] args) {
try {
int[][] arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
BufferedWriter bw = new BufferedWriter(new FileWriter("filename.txt"));
for (int[] row : arr) {
for (int value : row) {
bw.write(value + ",");
}
bw.newLine();
}
bw.close();
System.out.println("File written successfully");
} catch (IOException e) {
System.out.println("Error writing to file");
e.printStackTrace();
}
}
}
```
阅读全文