ArrayList的ArrayList。如何删除内部数组的项目?

时间:2014-10-22 10:45:41

标签: java arraylist

否则如果出现

的情况

整数ArrayList的ArrayList,我想只删除ArrayList中的一个索引?

例如:

           1  ->  [3,1,8,9]
ArrayList  2  ->  [0,2,4,5]
           3  ->  [9,3,4,5,5,9,0]

我希望删除ArrayList 2的第一个索引,即零(0)。

如果我使用

ArrayList.remove(2),它将删除所有第一个位置。那是[0,2,4,5]。但我只想删除第一个位置。数字0。

如何进行?

在这种情况下,我的块代码是:

   for (int i = 0; i < conta.size(); i++)
   {

     listaRepetida2[conta[i]].remove(conta[i+1]);
     listaRepetida2[conta[i]].remove(conta[i+1]);
     /*         
     listaRepetida2.get(conta.get(i)).remove(conta.get(i+1));
     listaRepetida2.get(conta.get(i)).remove(conta.get(i+1));
      */
     i+=2;
    }

conta,获取将从中删除的listaRepetida [i] [j]的索引。

3 个答案:

答案 0 :(得分:3)

你得到索引为1的内部列表:

List<Integer> innerList = outerList.get(1);

然后你删除它的第一个元素:

innerList.remove(0);

或者,在一条指令中:

outerList.get(1).remove(0);

答案 1 :(得分:0)

首先,您应该获取要从中删除元素的数组。你只需使用像这样的onstruct:

arr.get(2).remove(0);

答案 2 :(得分:0)

看到它;

我的代码(不要删除):

import java.util.ArrayList;

public class RemoveRepetidas
{
  ArrayList<ArrayList<Integer>> listaRepetida = new ArrayList<ArrayList<Integer>>();

  RemoveRepetidas (ArrayList<ArrayList<Integer>> _listaRepetida)
  {
    this.listaRepetida = _listaRepetida;    
  }


  String removeRepetida()
  {      
         System.out.println(this.listaRepetida);
         ArrayList<Integer> conta = new ArrayList<Integer>();         
         ArrayList<ArrayList<Integer>> listaRepetida2 = listaRepetida;


           for (int i = 0; i < listaRepetida2.size(); i++)
           {                 
              for (int j = i+1; j < listaRepetida2.size(); j++)
              {           
                   for (int k = 0; k < listaRepetida2.get(i).size(); k++)
                   {    
                       for (int l = 0; l < listaRepetida2.get(j).size(); l++)
                       {        
                           if (
                                ( listaRepetida2.get(i).get(k) == listaRepetida2.get(j).get(l) && listaRepetida2.get(i).get(k+1) == listaRepetida2.get(j).get(l+1) ) ||
                                ( listaRepetida2.get(i).get(k+1) == listaRepetida2.get(j).get(l) && listaRepetida2.get(i).get(k) == listaRepetida2.get(j).get(l+1) )
                              )
                              {
                                  conta.add(j);
                                  conta.add(l);
                              } 

                              l++;
                       }

                       k++;
                   } 
              }
            }

            for (int i = 0; i < conta.size(); i++)
            {
              //This does not remove
              listaRepetida2.get(conta.get(i)).remove(conta.get(i+1));
              listaRepetida2.get(conta.get(i)).remove(conta.get(i+1));
              i+=2;
            }

           String texto = "{";
           texto += "}";

           System.out.println(listaRepetida2);

           return texto;
  }
}