本文介绍了如何改变矢量到ArrayList的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发中的Java应用程序,自矢量
已经过时了,我需要这种改变使用的ArrayList
I am developing an application in Java and since Vector
is obsolete I am required to change this to using ArrayList
.
这是有关code需要被更改为的ArrayList
:
This is the relevant code that needs to be changed to ArrayList
:
这是豪斯医生类。
public Vector<Vector> getItems() {
Vector<Vector> data = new Vector<Vector>();
for (int i = 0; i < _itemList.size(); i++) {
Vector<String> row = new Vector<String>();
row.add(_itemList.get(i).getDecription());
row.add(String.valueOf(_itemList.get(i).getprice()));
data.add(row);
}
return data;
}
这是GUI类:
private void updateView() {
//Gets Rows and Columns from the House.class
Vector<Vector> rowData = _listener.getHouse().getItems();
Vector<String> columnNames = new Vector<String>();
columnNames.add("Product Name");
columnNames.add("Product Price(€)");
//Creates Shopping Cart and sets size + properties
table1 = new JTable(rowData, columnNames);
table1.setPreferredScrollableViewportSize(new Dimension(375, 325));
table1.setFillsViewportHeight(true);
//Adds ScrollPane to the container and sets the component position to center
JScrollPane scrollPane = new JScrollPane(table1);
centerPanel.add(scrollPane, BorderLayout.CENTER);
}
我需要完全停止VECTOR的使用量和使用ArrayList代替。有一个简单的方法是什么?如何取代这个什么方法?
I need to entirely stop the usage of VECTOR and use ArrayList instead. Is there a simple way out? Any ways on how to replace this?
推荐答案
这应该第一个做的。
public List<List<String>> getItems() {
List<List<String>> data = new ArrayList<ArrayList<String>>();
for (int i = 0; i < _itemList.size(); i++) {
List<String> row = new ArrayList<String>();
row.add(_itemList.get(i).getDecription());
row.add(String.valueOf(_itemList.get(i).getprice()));
data.add(row);
}
return data;
}
二是少一点微不足道的。你可以开始这样的事情,但我怀疑使用的TableModel
将是向前迈进了一大步。
The second is a little less trivial. You could start with something like this but I suspect using a TableModel
would be a good step forward.
private void updateView() {
//Gets Rows and Columns from the House.class
List<List<String>> rowData = _listener.getHouse().getItems();
List<String> columnNames = new ArrayList<String>();
columnNames.add("Product Name");
columnNames.add("Product Price(€)");
//Creates Shopping Cart and sets size + properties
// **** Will not work - Probably better to use a TableModel.
table1 = new JTable(rowData, columnNames);
table1.setPreferredScrollableViewportSize(new Dimension(375, 325));
table1.setFillsViewportHeight(true);
//Adds ScrollPane to the container and sets the component position to center
JScrollPane scrollPane = new JScrollPane(table1);
centerPanel.add(scrollPane, BorderLayout.CENTER);
}
这篇关于如何改变矢量到ArrayList的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!