2014-03-05 49 views
0

你好有什麼辦法將矢量轉換成arraylist?我想爲我的桌子搜索選項,但看起來對於我來說,使用ArrayList會更容易。我用搜索,但令人驚訝的是沒有關於矢量。轉換矢量爲Arraylist

public Vector<Vector<Object>> InfoForTheTable() { 
    Scanner s = null; 
    Vector<Vector<Object>> data = new Vector<Vector<Object>>(); 
    try { 
     s = new Scanner(new File("info.txt")); 
     while (s.hasNextLine()) { 
     String line = s.nextLine(); 
     if (line.startsWith("")) { 
      String[] atoms = line.split("[#]"); 
      Vector<Object> row = new Vector<Object>(); 
      row.add(atoms[0]); 
      row.add(atoms[1]); 
        row.add(atoms[2]); 
        row.add(atoms[3]); 
        row.add(atoms[4]); 
        row.add(atoms[5]); 
      data.add(row); 
     } 
     } 
    } 
    catch(IOException e) { 
     e.printStackTrace();  
    } 
    finally { 
     if (s != null) { 
     s.close(); 
     } 
    } 
    return data; 
    } 
+0

當前代碼有什麼問題? – Kick

+1

你有載體的矢量..它就像它是更易於你使用字典,甚至比'List of List ... ...並且不要使用使用 nachokk

+0

問題解決了,我用nachokk解決方案,其他解決方案也不錯 ! – user2121038

回答

2

我建議你不要使用該類型的集合,首先是ArrayList和Vector之間的主要區別是所有矢量操作都是同步的。其次,你有一個Vector< Vector<Object> >,你想這是ArrayList< ArrayList<Object> >因此,我認爲你應該創建自己的類(bean類)。

public class myTableModel{ 
private String somePropertyName1; 
. 
. 
. 
private String somePropertyNameN; 

public MyTableModel(String ... array){ 
    //assign values to instance attributes. 
} 
    //getters and setters 

} 


//remember method names in java starts with lower-case 
public List<MyTableModel> infoForTheTable() { 
    List<MyTableModel> data = new ArrayList<>(); //diamond inference 
    //use try-with-resources 
    try (Scanner s = new Scanner(Paths.getPath("info.txt"))){    
     while (s.hasNextLine()) { 
      String line = s.nextLine(); 
      if (line.startsWith("")) { 
      String[] atoms = line.split("[#]"); 
      data.add(new MyTableModel(atoms[0],atoms[1],atoms[2],atoms[3],atoms[4],atoms[5])); 
     } 
     } 
    } 
    catch(IOException e) { 
     //handle exception or throw it up! 
    } 
     return data; 
    } 
+0

另外:使用路徑而不是(遺留)文件。 – Puce

1

只使用這需要一個集合作爲其參數構造函數:

ArrayList<String> list = new ArrayList<String>(row); 

注意它只做一個淺拷貝。

詳細內容見this

+2

如下所示:http://stackoverflow.com/questions/3440082/java-vector-to-arraylist?rq=1 – Holloway

1

是,ArrayList有一個構造函數,需要一個Collection(這Vector顯然是)這樣做:

ArrayList(Collection<? extends E> c) 
2

而不是

Vector<Vector<Object>> data = new Vector<Vector<Object>>(); 

我建議使用:

List<List<Object>> data = new ArrayList<>(); 

甚至更​​好:

至少Java的1.7實例創建一個類MyInfo的和分配原子值到其屬性 然後使用:

List<MyInfo> data = new ArrayList<>(); 

因此,這裏是你的代碼的更現代的版本:

public List<Info> readInfoFromFile() { 
    List<Info> infoList= new ArrayList<>(); 
    try (Scanner s = new Scanner(Paths.get("info.txt")){ 
     while (s.hasNextLine()) { 
     String line = s.nextLine(); 
     if (line.startsWith("")) { 
      String[] atoms = line.split("[#]"); 
      Info info = new Info(); 
      info.setA(atoms[0]); 
      info.setB(atoms[1]); 
      info.setC(atoms[2]); 
      info.setD(atoms[3]); 
      info.setE(atoms[4]); 
      info.setG(atoms[5]); 
      infoList.add(rowinfo); 
     } 
    } catch(IOException e) { 
     e.printStackTrace();  
    } 
    return data; 
} 

根據需要更換的特性和它們的類型。

+0

+1是的,這是更正確的,我沒有閱讀我將編輯的代碼 – nachokk