sách gpt4 ai đã đi

java - 如何从另一个 .java 文件引用不同 .java 文件中的数组?

In lại 作者:行者123 更新时间:2023-12-01 12:02:17 27 4
mua khóa gpt4 Nike

我很困惑。我想向库中添加更多数据,但是我想从定义库的另一个 .java 文件收集数据。

库代码如下:

private static Map library = new TreeMap();

static {

library.put("01", new Item("Chris@redwich.ac.uk", "Hello",
"Kate@redwich.ac.uk", "How is the course going?", 2));
library.put("02", new Item("Kate@redwich.ac.uk", "Re: Hello",
"Chris@redwich.ac.uk", "> How is the course going?\n\nBrilliant, thanks. The students are all fantastic and are going to get top marks in their coursework.", 2));
library.put("03", new Item("A.Friend@hmail.com", "Coffee",
"Chris@redwich.ac.uk", "You're working too hard - fancy meeting for coffee?.", 5));
library.put("04", new Item("Chris@redwich.ac.uk", "Exam",
"Asif@redwich.ac.uk", "I have nearly finished writing the exam - I hope the students have revised hard.", 4));
library.put("05", new Item("A.Student@redwich.ac.uk", "Timetable",
"Chris@redwich.ac.uk", "help!!! my timetable is rubbish - i cant understand it!!! what r u going to do?", 0));
library.put("06", new Item("Chris@redwich.ac.uk", "Re: Timetable",
"A.Student@redwich.ac.uk", "Please ignore the timetables on the portal - just follow the advice on TeachMat.", 0));
library.put("07", new Item("A.Student@redwich.ac.uk", "Re: Timetable",
"Chris@redwich.ac.uk", "thx :)", 0));

我希望从另一个 .java 文件代码导入的数据如下所示:

public String[] MessageData2(){
String messageId = messageIdText.getText();
String subject = subjectText.getText();
String recipient = recipientText.getText();
String message = messageArea.getText();
String [] messageData1 = {"0","1","2"};
messageData1 [0] = messageId;
messageData1 [1] = subject;
messageData1 [2] = recipient;
messageData1 [3] = message;

return messageData1;

它需要遵循与之前相同的约定。我尝试做的是这样的:

library.put(messageData1[0], new Item(messageData1[2], messageData1[1],
messageData1[2], messageData1[3], 0));

有人可以帮忙吗?

1 Câu trả lời

//在单独的文件 classA.java 中

public classA
{
// always when creating a classA object a empty TreeMap (your decision)
private Map library = new TreeMap();

public setLibrary(Map library)
{
this.libray = libray
}
public getLibrary()
{
return this.library;
}
}

现在您可以创建一个对象,例如:

classA a = new ClassA();
a.getLibrary.put(....);

这实际上取决于你的整个结构。但 setter/getter 是一个基本概念。

关于java - 如何从另一个 .java 文件引用不同 .java 文件中的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27906286/

27 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com