ArrayList dynamic two-dimensional array (delete create increased sentinel read modify)

 Brush on leetcode a question about the two-dimensional array, the title itself is not difficult, but I was stuck in for the use of ArrayList, so write a blog about it.

 

create

The first is to create a two-dimensional array initialization ArrayList:

List<List<元素类型>> 数组名=new ArrayList<List<元素类型>>();

例如:
List<List<Integer>> re=new ArrayList<List<Integer>>();

 

Increase Delete:

Increase, we mainly through the establishment of an array of one-dimensional, two-dimensional to add, delete also empathy

增加 

 List<Integer> FirstRow=new ArrayList<Integer>();      //新建一维数组
 FirstRow.add(1);  // 给一维数组加入元素
 re.add(FirstRow);  //将一维数组加入二维数组

删除

 //remove有两个重载方法
remove(int index)
remove(Object o)

remove(1)   //是删除索引为1的元素
remove(new Integer(1))    //则删除元素1

 

 

Fixed reading

 

I mainly stuck in this step, I know you want to read one-dimensional function Area with a use is get (int num), num indicate which one you want to read, I thought it was two-dimensional, get (int row, int col), for example, I want to read two-dimensional array [1,2] element location, it is a get (1,2), but in fact not the case, the real way is this call

re.get(1).get(2)    //先get行号,后get 列号

Designated modification

对于一维的ArrayList 修改方法为set

demo.set(int index 要修改的索引值, 要修改的值)

二维的话
只需要 

re.get(i 行号).set(j 列号, 要修改的值)

 

Guess you like

Origin blog.csdn.net/Lin_QC/article/details/93484948