public class Book { public string file = "..\\..\\..\\..\\book.txt"; // 文件路径 public void book() { string[] lines = File.ReadAllLines(file); // 读取文件中的每一行数据 int rowCount = lines.Length; // 行数 int colCount = lines[0].Split(',').Length;// 列数(假设每行数据的列数相同) //定义二维数组 string[,] arr = new string[rowCount, colCount]; for (int i = 0; i < rowCount; i++) { string[] values = lines[i].Split(','); // 分隔每行数据 for (int j = 0; j < colCount; j++) { arr[i, j] = values[j]; // 将每个值转换为字符串并存入二维数组中 } } } }如何在另一个类中访问二维数组arr
时间: 2024-01-12 09:02:35 浏览: 31
有两种方法可以在另一个类中访问二维数组arr:
1. 将二维数组arr声明为public static类型,这样就可以在其他类中通过类名直接访问该数组:
```
public class Book {
public static string[,] arr;
//...
}
public class OtherClass {
public void AccessArray() {
// 访问Book类中的二维数组arr
string value = Book.arr[0, 0];
}
}
```
2. 在Book类中定义一个public方法,该方法返回二维数组arr,然后在其他类中调用该方法获取数组:
```
public class Book {
private string[,] arr;
//...
public string[,] GetArray() {
return arr;
}
}
public class OtherClass {
public void AccessArray() {
Book book = new Book();
string[,] arr = book.GetArray(); // 获取Book类中的二维数组arr
string value = arr[0, 0];
}
}
```
相关问题
java.lang.NoClassDefFoundError: jxl/WorkbookSettings
这个错误通常是由于缺少jxl库文件或者类路径不正确导致的。你可以尝试以下几个步骤来解决这个问题:
1. 确认你已经正确地添加了jxl库文件。你可以在项目的构建路径中添加jxl.jar文件,或者将jxl.jar文件复制到项目的lib目录下。
2. 确认你的类路径正确。你可以在命令行中使用java -cp命令来指定类路径,或者在Eclipse中设置项目的类路径。
3. 确认你的jxl库文件版本正确。如果你的代码使用的是旧版本的jxl库文件,而你安装的是新版本的jxl库文件,那么就会出现这个错误。你可以尝试使用旧版本的jxl库文件来解决这个问题。
以下是一个可能的解决方案:
```java
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class ReadExcel {
public static void main(String[] args) {
try {
File file = new File("/mnt/sdcard/Pictures/MyPictures/测试数据.xls");
FileInputStream fis = new FileInputStream(file);
Workbook book = Workbook.getWorkbook(fis);
int num_sheet = book.getNumberOfSheets();
System.out.println(String.valueOf(num_sheet));
book.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
}
}
}
```
写代码android studio代码记事本,文件名是book1版本是2022.2.1
很好,以下是在 Android Studio 中创建一个名为 "book1" 的记事本应用程序的步骤,版本为 2022.2.1:
1. 打开 Android Studio,点击 "Start a new Android Studio project"。
2. 在 "Choose your project" 窗口中,选择 "Empty Activity",然后点击 "Next"。
3. 在 "Configure your project" 窗口中,输入 "book1" 作为应用程序的名称,选择 "Java" 作为语言,选择 "API 30: Android 11.0 (R)" 作为最低 SDK 版本,然后点击 "Finish"。
4. 在项目结构中,找到 "MainActivity.java" 文件,并用下面的代码替换它:
```
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
EditText editText;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
textView = findViewById(R.id.textView);
Button saveButton = findViewById(R.id.saveButton);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveToFile();
}
});
Button loadButton = findViewById(R.id.loadButton);
loadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
loadFromFile();
}
});
}
private void saveToFile() {
try {
String text = editText.getText().toString();
File file = new File(getFilesDir(), "book1.txt");
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(text.getBytes());
outputStream.close();
textView.setText("Text saved to " + file.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
}
private void loadFromFile() {
try {
File file = new File(getFilesDir(), "book1.txt");
FileInputStream inputStream = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder builder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
builder.append(line).append("\n");
}
reader.close();
inputStream.close();
textView.setText(builder.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
5. 在 "res/layout" 目录下,找到 "activity_main.xml" 文件,并用下面的代码替换它:
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:hint="Enter text here..." />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/saveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save" />
<Button
android:id="@+id/loadButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Load" />
</LinearLayout>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="No text loaded." />
</LinearLayout>
```
6. 运行应用程序并测试它是否正常工作。现在,您应该可以输入文本并将其保存到名为 "book1.txt" 的文件中,然后从文件中加载文本。