1

我正在用 Java 开发一个应用程序,由于Vector它已经过时,我需要将其更改为 using ArrayList.

这是需要更改为的相关代码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。有没有简单的出路?关于如何替换它的任何方法?

4

2 回答 2

1
Vector<String> vector = new Vector<String>();
// (... Populate vector here...)
ArrayList<String> list = new ArrayList<String>(vector);

这是从这里java vector 到 arraylist

于 2013-05-09T11:41:22.087 回答
1

这应该适用于第一个。

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;
}

第二个不那么琐碎。你可以从这样的事情开始,但我怀疑使用 aTableModel会是一个很好的进步。

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);
}
于 2013-05-09T11:37:18.123 回答