![](/img/trans.png)
[英]How to replace contents of arraylist with the contents of another arraylist?
[英]How to check against the contents of an arraylist then get the value of another element of the arraylist
我有一個GUI,需要用戶輸入。 通過輸入,我將其添加到分配給類的arraylist中。 因此,我需要某些元素以確保它適合構造函數。 但是,輸入並沒有滿足構造函數所需的一切。 我需要查看用戶輸入的一部分是否與我在arraylist中已經擁有的內容相同。 如果匹配,則需要從該對象獲取價格以用於填充構造函數。 以下是我目前擁有的動作偵聽器代碼;
SubmitButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
String data[];
String data1[];
double price;
String cType;
if((CustomerTest.courseList.contains(courseBox.getSelectedItem().toString())))
{
return CustomerTest.courseList.get(2);
}
data = startDateField.getText().split("/");
data1 = endDateField.getText().split("/");
Course course1 = new Course(courseBox.getSelectedItem().toString(), instructorField.getText(),**price** **This is the variable I need from the arraylist as prices will be the same for the classes**, new Date(Integer.parseInt(data[0]),
Integer.parseInt(data[1]),Integer.parseInt(data[2])),new Date(Integer.parseInt(data1[0]),
Integer.parseInt(data1[1]),Integer.parseInt(data1[2])));
CustomerTest.courseList.add(course1);
for (Course c: CustomerTest.courseList)
System.out.println(c.toString());
如果您需要其他信息以幫助您,請告訴我。 我真的很感激!
不確定,但是可能在ArrayList上使用for循環? 您也可以嘗試使用一些輸出來調試它:
例如System.out.println(courseBox.getSelectedItem()。toString());
for(int i = 0; i < CustomerTest.courseList.size(); i++) {
if((CustomerTest.courseList.contains(courseBox.getSelectedItem().toString()))){
return CustomerTest.courseList.get(2);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.