[英]How can join multiple arraylists
I have two different arraylists.我有两个不同的数组列表。 In the first, there are vars (title,author,isbn,date,kind).首先,有变量(标题、作者、isbn、日期、种类)。 In the second there are (title,author,isbn,date,kind,field) such as Strings.第二个有(title,author,isbn,date,kind,field),比如Strings。 I dont use super class or subclass.Finally i want these arraylists to populate in common jtable When i run the program with the first arraylist i take this result 1st_arraylist When i run the program with the second arraylist i take this result 2st_arraylist When i run the program both of two arraylists i take this result both arraylists I want to view both arraylists in one jtable My code I dont use super class or subclass.Finally i want these arraylists to populate in common jtable When i run the program with the first arraylist i take this result 1st_arraylist When i run the program with the second arraylist i take this result 2st_arraylist When i run the对两个数组列表都进行编程我得到这个结果两个数组列表我想在一个中查看两个数组列表 jtable 我的代码
public class LiteraryBookList {
private ArrayList<LiteraryBook> LibookList;
public LiteraryBookList() {
LibookList = new ArrayList<LiteraryBook>();
}
public void add(LiteraryBook lb) {
LibookList.add(lb);
}
public ArrayList<LiteraryBook> getBooks() {
return LibookList;
}
public void readFromTxt(String filename) {
File file = new File(filename);
FileReader reader = null;
try {
reader = new FileReader(file);
} catch (FileNotFoundException e) {
System.exit(1);
}
BufferedReader infile = new BufferedReader(reader);
String line = "";
int counter = 0;
String title = "";
String author = "";
String isbn = "";
String date = "";
String kind = "";
try {
while ((line = infile.readLine()) != null) {
if (line.isEmpty() || line.contains("1")) {
continue;
}
counter++;
if (counter == 1) {
title = line;
} else if (counter == 2) {
author = line;
} else if (counter == 3) {
isbn = line;
} else if (counter == 4) {
date = line;
} else if (counter == 5) {
kind = line;
LiteraryBook lb = new LiteraryBook(title, author, isbn, date, kind);
LibookList.add(lb);
counter = 0;
}
}
} catch (IOException ex) {
Logger.getLogger(LiteraryBookList.class.getName()).log(Level.SEVERE, null, ex);
}
}
public Object[][] convert2Data() {
Object[][] data = new Object[LibookList.size()][5];
for (int i = 0; i < LibookList.size(); i++) {
data[i][0] = LibookList.get(i).getTitle();
data[i][1] = LibookList.get(i).getAuthor();
data[i][2] = LibookList.get(i).getIsbn();
data[i][3] = LibookList.get(i).getDate();
data[i][4] = LibookList.get(i).getKind();
}
return data;
}
}
Also i have..我也有..
public class ScientificBookList {
private ArrayList<ScientificBook> ScibookList;
public ScientificBookList() {
ScibookList = new ArrayList<ScientificBook>();
}
public void add(ScientificBook sb) {
ScibookList.add(sb);
}
public ArrayList<ScientificBook> getBooks() {
return ScibookList;
}
public void readFromTxt(String filename) {
File file = new File(filename);
FileReader reader = null;
try {
reader = new FileReader(file);
} catch (FileNotFoundException e) {
System.exit(1);
}
BufferedReader infile = new BufferedReader(reader);
String line = "";
int counter = 0;
String sci_title = "";
String sci_author = "";
String sci_isbn = "";
String sci_date = "";
String sci_kind = "";
String scientific_field = "";
try {
while ((line = infile.readLine()) != null) {
if (line.isEmpty() || line.contains("2")) {
continue;
}
counter++;
if (counter == 1) {
sci_title = line;
} else if (counter == 2) {
sci_author = line;
} else if (counter == 3) {
sci_isbn = line;
} else if (counter == 4) {
sci_date = line;
} else if (counter == 5) {
sci_kind = line;
} else if (counter == 6) {
scientific_field = line;
ScientificBook sb = new ScientificBook(sci_title, sci_author, sci_isbn, sci_date, sci_kind,scientific_field);
ScibookList.add(sb);
counter = 0;
}
}
} catch (IOException ex) {
Logger.getLogger(LiteraryBookList.class.getName()).log(Level.SEVERE, null, ex);
}
}
public Object[][] convert2Data() {
Object[][] data = new Object[ScibookList.size()][6];
for (int i = 0; i < ScibookList.size(); i++) {
data[i][0] = ScibookList.get(i).getSci_Title();
data[i][1] = ScibookList.get(i).getSci_Author();
data[i][2] = ScibookList.get(i).getSci_Isbn();
data[i][3] = ScibookList.get(i).getSci_Date();
data[i][4] = ScibookList.get(i).getSci_Kind();
data[i][5] = ScibookList.get(i).getScientific_field();
}
return data;
}
}
Finally i have最后我有
public class ShowBooks extends JFrame {
private Object[][] data;
private String[] columnNames = {"Title", "Author", "Isbn", "Date", "Kind", "Field"};
private DefaultTableModel tableModel;
private JTable table;
private LiteraryBookList myList1;
private ScientificBookList myList2;
public ShowBooks() {
setBounds(10, 10, 400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myList1 = new LiteraryBookList();
myList2 = new ScientificBookList();
myList1.readFromTxt("test.txt");
myList2.readFromTxt("test.txt");
data = myList1.convert2Data();
data = myList2.convert2Data();
tableModel = new DefaultTableModel(data, columnNames);
table = new JTable(tableModel);
table.setAutoCreateRowSorter(true);
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.setPreferredSize(new Dimension(380, 280));
JPanel panel = new JPanel();
panel.add(scrollPane);
add(panel, BorderLayout.CENTER);
}
}
This could be a dirty solution somewhat duplicating convert2Data
methods which should not be called at all:这可能是一个肮脏的解决方案,在某种程度上重复了根本不应该调用的convert2Data
方法:
// prepare one large array to contain both types of books
data = new Object[myList1.getBooks().size() + myList2.getBooks().size()][6];
int row = 0;
// fill 2D array with scientific books' data
for (ScientificBook book : myList1.getBooks()) {
data[row][0] = book.getSci_Title();
data[row][1] = book.getSci_Author();
data[row][2] = book.getSci_Isbn();
data[row][3] = book.getSci_Date();
data[row][4] = book.getSci_Kind();
data[row][5] = book.getScientific_field();
row++;
}
// fill 2D array with literary books' data
for (LiteraryBook book : myList2.getBooks()) {
data[row][0] = book.getTitle();
data[row][1] = book.getAuthor();
data[row][2] = book.getIsbn();
data[row][3] = book.getDate();
data[row][4] = book.getKind();
data[row][5] = "";
row++;
}
Though it's highly recommended to redesign your classes and remove duplicated/ unnecessary code.尽管强烈建议重新设计您的类并删除重复/不必要的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.