Java - Instanciar objetos ArrayList

 
Vista:

Instanciar objetos ArrayList

Publicado por Duilio Bacalor (1 intervención) el 29/01/2009 22:07:58
Hola , tengo una duda respecto a la utilizacion de la clase ArrayList , es porque en varios ejemplos e incluso tutoriales se muestra y se dice que lo correcto es:
List b;
b= new ArrayList();

y no

ArrayList b;

b= new ArrayList() ;


No encuentro la razon para esto si alguno podria por fabor explicarme o decirme donde puedo leer sobre esto le agradeceria muchisimo.
Saludos
Duilio
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:Instanciar objetos ArrayList

Publicado por webness (57 intervenciones) el 29/01/2009 22:24:47
Lo que pasa es que List es una interface y ArrayList es una clase que implementa List por lo tanto se puede deducir que para crear un objeto List toca obligado implementar codigo en todos sus metodos de interface, este codigo lo hace ArrayList:

Tu puede personalizar tu objeto List directamente creando tu propio codigo, seria poner tu codigo en el interior de cada uno de los siguientes metodos:

List list= new List() {

public int size()
{
throw new UnsupportedOperationException("Not supported yet.");
}

public boolean isEmpty()
{
throw new UnsupportedOperationException("Not supported yet.");
}

public boolean contains(Object o)
{
throw new UnsupportedOperationException("Not supported yet.");
}

public Iterator iterator()
{
throw new UnsupportedOperationException("Not supported yet.");
}

public Object[] toArray()
{
throw new UnsupportedOperationException("Not supported yet.");
}

public Object[] toArray(Object[] a)
{
throw new UnsupportedOperationException("Not supported yet.");
}

public boolean add(Object o)
{
throw new UnsupportedOperationException("Not supported yet.");
}

public boolean remove(Object o)
{
throw new UnsupportedOperationException("Not supported yet.");
}

public boolean containsAll(Collection c)
{
throw new UnsupportedOperationException("Not supported yet.");
}

public boolean addAll(Collection c)
{
throw new UnsupportedOperationException("Not supported yet.");
}

public boolean addAll(int index, Collection c)
{
throw new UnsupportedOperationException("Not supported yet.");
}

public boolean removeAll(Collection c)
{
throw new UnsupportedOperationException("Not supported yet.");
}

public boolean retainAll(Collection c)
{
throw new UnsupportedOperationException("Not supported yet.");
}

public void clear()
{
throw new UnsupportedOperationException("Not supported yet.");
}

public Object get(int index)
{
throw new UnsupportedOperationException("Not supported yet.");
}

public Object set(int index, Object element)
{
throw new UnsupportedOperationException("Not supported yet.");
}

public void add(int index, Object element)
{
throw new UnsupportedOperationException("Not supported yet.");
}

public Object remove(int index)
{
throw new UnsupportedOperationException("Not supported yet.");
}

public int indexOf(Object o)
{
throw new UnsupportedOperationException("Not supported yet.");
}

public int lastIndexOf(Object o)
{
throw new UnsupportedOperationException("Not supported yet.");
}

public ListIterator listIterator()
{
throw new UnsupportedOperationException("Not supported yet.");
}

public ListIterator listIterator(int index)
{
throw new UnsupportedOperationException("Not supported yet.");
}

public List subList(int fromIndex, int toIndex)
{
throw new UnsupportedOperationException("Not supported yet.");
}
}

Ahora un ArrayList es esto mismo, pero para no hacer todo esto, se creo una clase aparte llamada arrayList.

Lo mismo sucede con los eventos.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:Instanciar objetos ArrayList

Publicado por adrian (1 intervención) el 22/01/2015 13:13:49
List es la interface de ArrayList (es la implementación)
Map es la interface de HashMap

Instanciar:

List<Object> list = new ArrayList<Object>();
Map<Object> map= new HashMap<Object>();
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar