2012-04-07 18 views

回答

1

使用ArrayList中您可以:

import java.util.ArrayList; 

public class ArrayList2d<Type> 
{ 
ArrayList<ArrayList<Type>> array; 

public ArrayList2d() 
{ 
    array = new ArrayList<ArrayList<Type>>(); 
} 

public void ensureCapacity(int num) 
{ 
    array.ensureCapacity(num); 
} 


public void ensureCapacity(int row, int num) 
{ 
    ensureCapacity(row); 
    while (row < getNumRows()) 
    { 
     array.add(new ArrayList<Type>()); 
    } 
    array.get(row).ensureCapacity(num); 
} 


public void Add(Type data, int row) 
{ 
    ensureCapacity(row); 
    while(row >= getNumRows()) 
    { 
     array.add(new ArrayList<Type>()); 
    } 
    array.get(row).add(data); 
} 

public Type get(int row, int col) 
{ 
    return array.get(row).get(col); 
} 

public void set(int row, int col, Type data) 
{ 
    array.get(row).set(col,data); 
} 

public void remove(int row, int col) 
{ 
    array.get(row).remove(col); 
} 

public boolean contains(Type data) 
{ 
    for (int i = 0; i < array.size(); i++) 
    { 
     if (array.get(i).contains(data)) 
     { 
      return true; 
     } 
    } 
    return false; 
} 

public int getNumRows() 
{ 
    return array.size(); 
} 

public int getNumCols(int row) 
{ 
    return array.get(row).size(); 
} 
} 
+0

我相信OP意味着一個'JList'。 – Jeffrey 2012-04-07 21:49:26

+0

@Jeffrey對不起..!:(我無法滿足你的需求..! – SenthilPrabhu 2012-04-07 23:26:00

相關問題