大家好,我是Java新手,我正在尝试将一个ArrayList克隆到驻留在另一个类中的另一个ArrayList中,下面是我的www.example.com中的原始ArrayList的代码TaskPage.java:
public ArrayList<String> tName = new ArrayList<>();
public ArrayList<String> tDesc = new ArrayList<>();
public ArrayList<String> fLName = new ArrayList<>();
public ArrayList<String> tStatus = new ArrayList<>();
public ArrayList<Integer> tDuration = new ArrayList<>();
public ArrayList<String> tID = new ArrayList<>();
String TName = taskName.getText();
tName.add(TName);
String TDescription = taskDescription.getText();
tDesc.add(TDescription);
String FName = FirstName.getText();
String LName = LastName.getText();
fLName.add(FName + " " + LName);
int Duration = Integer.parseInt(taskDuration.getText());
tDuration.add(Duration);
String taskID;
String taskNumber = "";
Random rand = new Random();
int rNum = rand.nextInt(21);
taskNumber = "0" + Integer.toString(rNum);
TaskClass tClass = new TaskClass();
taskID = tClass.createTaskID(TName, taskNumber, FName);
tID.add(taskID);
下面是www.example.com的代码Report.java:
public class Report {
TaskPage tPage = new TaskPage();
public ArrayList<String> taskName;
public ArrayList<String> taskDesc;
public ArrayList<String> fLName;
public ArrayList<String> taskStatus;
public ArrayList<String> taskDuration;
public ArrayList<String> taskID;
public String res = "";
public void init() {
taskName = (ArrayList)tPage.tName.clone();
taskDesc = (ArrayList)tPage.tDesc.clone();
fLName = (ArrayList)tPage.fLName.clone();
taskStatus = (ArrayList)tPage.tStatus.clone();
taskDuration = (ArrayList)tPage.tDuration.clone();
taskID = (ArrayList)tPage.tID.clone();
}
public void statusDone() {
init();
for (int k=0; k <= tPage.tName.size(); k++) {
if (tPage.tStatus.get(k) == "Done") {
res = tPage.fLName.get(k) + ", " + tPage.tName.get(k) + ", " + tPage.tDuration.get(k) + "\n";
} else {
res = "There is nothing to display here!";
}
}
}
}
最后是我的www.example.com的代码ComingSoon.java:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
Report rpt = new Report();
rpt.statusDone();
String Status = rpt.res;
JOptionPane.showMessageDialog(null, Status);
}
当我运行代码时,它给出了错误:
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)
at java.base/java.util.Objects.checkIndex(Objects.java:359)
at java.base/java.util.ArrayList.get(ArrayList.java:427)
at userlogin.Report.statusDone(Report.java:36)
at userlogin.ComingSoon.jButton1ActionPerformed(ComingSoon.java:188)
at userlogin.ComingSoon$1.actionPerformed(ComingSoon.java:54)
at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972)
at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2313)
at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
at java.desktop/java.awt.Component.processMouseEvent(Component.java:6626)
at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3389)
at java.desktop/java.awt.Component.processEvent(Component.java:6391)
at java.desktop/java.awt.Container.processEvent(Container.java:2266)
at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5001)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2324)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4948)
at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4575)
at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4516)
at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2310)
at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2780)
at java.desktop/java.awt.Component.dispatchEvent(Component.java:4833)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:722)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:716)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:97)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:746)
at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:744)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:743)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
1条答案
按热度按时间zbdgwd5y1#
您的代码中存在许多问题,但您得到ArrayIndexOutOfBoundsException的原因是
statusDone()
函数中的for循环错误。k = tPage.tName.size()
时出现问题。由于数组(和ArrayLists)是0索引的,
k-1
处的元素是最后一个元素,因此tPage.tStatus.get(k)
引发ArrayIndexOutOfBoundsException。您应该使用<
而不是<=
。另一个可能的问题是,你似乎没有在任何地方调用
init()
,我假设init()
应该在statusDone()
之前调用。有关提高可读性和可维护性的其他建议:
Task
类,并将6个列表替换为ArrayList<Task>
(感谢Dave Newton),而不是包含6个字符串的ArrayListnew ArrayList<>(tPage.tName)
而不是(ArrayList)tPage.tName.clone()
。通常不鼓励在集合上使用clone()
,并且强制转换它也不是很干净。init()
中的所有内容都移到构造函数中。当您调用new Report()
时,代码就开始运行