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 12:02:35 浏览: 79
有两种方法可以在另一个类中访问二维数组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];
}
}
```
相关问题
package 作业5; import java.awt.; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.; import java.io.File; import java.util.HashMap; import java.util.Scanner; import java.io.File; import java.util.HashMap; import java.util.Scanner; public class Example13_7{ public static void main( String args []){ WindowWord win = new WindowWord(); win.setTitle("英﹣汉小字典"); } } class WindowWord extends JFrame{ JTextField inputText,showText; WordPolice police; public WindowWord(){ setLayout(new FlowLayout()); inputText = new JTextField(6); showText = new JTextField(6); showText.setColumns(15); add(inputText); add(showText); police = new WordPolice(); police.setView(this); inputText.addActionListener(police); setBounds(100,100,400,280); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } class WordPolice implements ActionListener { WindowWord view; HashMap<String,String>hashtable; File file = new File ("E:\JAVA\上机\作业5\cet4.txt"); Scanner sc = null ; WordPolice () { hashtable = new HashMap< String,String >(); //System.out.println(hashtable.size()); try { sc = new Scanner ( file ); //System.out.println(sc); while (sc.hasNext ()) { String englishWord = sc . next (); String chineseWord = sc . next (); hashtable . put ( englishWord , chineseWord ); } HashMap<String,String> reversedHashtable = new HashMap<String,String>(); for (String key : hashtable.keySet()) { String value = hashtable.get(key); reversedHashtable.put(value, key); } hashtable.putAll(reversedHashtable); } catch(Exception e ) {} } public void setView(WindowWord view) { this.view = view; } public void actionPerformed(ActionEvent e) { String inputWord = view.inputText.getText (); if(hashtable.containsKey(inputWord)) { String outputWord = hashtable.get ( inputWord ); view.showText.setText (outputWord ); } else { String chineseWord = inputWord; for (String key : hashtable.keySet()) { String value = hashtable.get(key); if (value.equals(chineseWord)) { view.showText.setText(key); return; } } for (String value : hashtable.values()) { if (value.equals(inputWord)) { String outputWord = hashtable.get(value); view.showText.setText(outputWord); return; } } view.showText.setText ("没有此单词"); } } }我在输入框输入“书”,但结果是没有此单词,但是输入“book”,结果是“n.书,书籍”,如何解决
你的程序中只是将英文单词和对应的中文翻译存储在了HashMap中,没有对输入的中文单词进行处理。所以当你输入“书”时,程序无法匹配到对应的英文单词,因此输出“没有此单词”。
要解决这个问题,你可以在程序中增加一个判断:当输入的单词不是英文单词时,先将其作为中文单词进行匹配。具体来说,可以在actionPerformed方法中增加以下代码:
```java
if(!inputWord.matches("[a-zA-Z]+")) { // 如果输入的单词不是英文单词
for (String value : hashtable.values()) { // 遍历HashMap中的所有中文翻译
if (value.equals(inputWord)) { // 如果找到了匹配的中文翻译
String outputWord = hashtable.get(value); // 获取对应的英文单词
view.showText.setText(outputWord); // 显示翻译结果
return;
}
}
}
```
这段代码的作用是判断输入的单词是否是英文单词(即是否只包含字母),如果不是,则遍历HashMap中的所有中文翻译,找到与输入单词相匹配的中文翻译,并获取对应的英文单词,最后显示翻译结果。
加入这段代码后,你再输入“书”,就能够正确地显示出“n.书,书籍”这个翻译结果了。
基于文档代码实现: 1.连接上project/src/com/zrgj/Book.txt和project/src/com/zrgj/Reader.txt,再进行查看待借书籍功能、预约图书功能、还书功能
为了实现与 `project/src/com/zrgj/Book.txt` 和 `project/src/com/zrgj/Reader.txt` 文件的连接,并实现查看待借书籍、预约图书和还书的功能,我们需要对现有的代码进行一些调整和补充。以下是详细的实现步骤:
### 1. 连接文件路径
在 `Demo` 类中,我们已经创建了 `Reader.txt` 和 `Book.txt` 文件。现在需要确保这些文件的路径与 `Tools` 类中的路径一致。
```java
public class Demo {
public static void main(String[] args) throws IOException {
ArrayList<Reader> readers = new ArrayList<>();
Path projectPath = Paths.get(System.getProperty("user.home"), "project", "src", "com", "zrgj");
File readerFile = new File(projectPath.toString(), "Reader.txt");
File bookFile = new File(projectPath.toString(), "Book.txt");
if (!readerFile.exists()) {
readerFile.createNewFile();
System.out.println("文件 " + readerFile.getAbsolutePath() + " 已创建");
}
if (!bookFile.exists()) {
bookFile.createNewFile();
System.out.println("文件 " + bookFile.getAbsolutePath() + " 已创建");
}
readers = Tools.strToArr(Tools.readFile(readerFile.getAbsolutePath()));
boolean t = true;
Scanner sc = new Scanner(System.in);
while (t) {
System.out.println("欢迎使用图书馆管理系统");
System.out.println("1. 注册");
System.out.println("2. 登录");
System.out.println("3. 退出系统");
System.out.print("请选择操作:");
int choice = sc.nextInt();
sc.nextLine();
switch (choice) {
case 1:
registerReader(readers, readerFile);
break;
case 2:
loginReader(readers, readerFile);
break;
case 3:
System.out.println("正在退出系统...");
System.out.println("系统退出成功");
System.exit(0);
break;
default:
System.out.println("输入错误,请重试");
break;
}
}
}
// 其他方法保持不变
}
```
### 2. 实现查看待借书籍功能
在 `Demo` 类中,已经有一个 `viewAvailableBooks` 方法。我们需要确保这个方法能够正确读取 `Book.txt` 文件并显示可用的书籍。
```java
public static void viewAvailableBooks(File bookFile) throws IOException {
System.out.println("--查看待借书籍-----");
ArrayList<ReservationBook> books = Tools.readBooksFromFile();
if (books == null || books.isEmpty()) {
System.out.println("当前没有可用的书籍。");
return;
}
for (ReservationBook book : books) {
if (book.getReaderid().isEmpty()) {
System.out.println(book.getBookname() + " (ID: " + book.getBookid() + ")");
}
}
}
```
### 3. 实现预约图书功能
在 `Demo` 类中,已经有一个 `reserveBook` 方法。我们需要确保这个方法能够正确读取 `Book.txt` 文件并更新预约状态。
```java
public static void reserveBook(ArrayList<Reader> readers, File bookFile) {
System.out.println("-----预约图书-----");
Scanner sc = new Scanner(System.in);
System.out.print("请输入书号:");
String b_id = sc.next();
System.out.print("请输入读者号:");
String r_id = sc.next();
try {
if (isBookAvailable(b_id)) {
Tools.markBookAsReserved(b_id, r_id);
System.out.println("预约成功");
} else {
System.out.println("该图书已被借阅,无法预约");
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static boolean isBookAvailable(String b_id) throws IOException {
ArrayList<ReservationBook> books = Tools.readBooksFromFile();
for (ReservationBook book : books) {
if (book.getBookid().equals(b_id) && book.getReaderid().isEmpty()) {
return true;
}
}
return false;
}
```
### 4. 实现还书功能
在 `Demo` 类中,已经有一个 `returnBook` 方法。我们需要确保这个方法能够正确读取 `Book.txt` 文件并更新还书状态。
```java
public static void returnBook(ArrayList<Reader> readers, File bookFile) {
System.out.println("-----还书-----");
Scanner sc = new Scanner(System.in);
System.out.print("请输入书号:");
String b_id = sc.next();
try {
Tools.markBookAsUnreserved(b_id);
System.out.println("还书成功");
} catch (IOException e) {
e.printStackTrace();
}
}
```
### 总结
通过以上步骤,我们已经实现了与 `project/src/com/zrgj/Book.txt` 和 `project/src/com/zrgj/Reader.txt` 文件的连接,并实现了查看待借书籍、预约图书和还书的功能。这些功能的核心在于正确读取和写入文件,以及更新文件中的数据。
阅读全文
相关推荐














