Separate the values from List

0

I'm using List to sort some values, but I need to separate them after I've manipulated the values.

List<Map.Entry<String,Integer>> lista = new ArrayList<Map.Entry<String,Integer>>(fila);

And for printing I'm using: System.out.println(lista);

And so, it returns me all elements of the list. [c=30, e=25, b=20] .

My intention would be to pass this to a vector, for example, because my list has a small size (as in case 3). For if passing to a vector, I can only access the position I want.

    
asked by anonymous 25.09.2014 / 16:48

3 answers

4

A java.util.List can be accessed directly by the index, as below:

List<Integer> list = ...
Integer first = list.get(0);
Integer last = list.get(list.size() - 1);

But if you still want to turn it into an array , use:

Object[] array = list.toArray();

or

Integer[] array = list.toArray(new Integer[0]);

Reference and more infor- mation: link

    
25.09.2014 / 17:17
1

To return a given value from a list position you can execute the following code:

System.out.println (list.get (index));

I think you do not need to pass your list to a vector because a vector only "works" with a data type, but in case you have a MAP works with the concept of key-value, key and associated value.

    
25.09.2014 / 17:02
0

Do not use vector, use maps (Map) or implement hashCode and and equals of the object and work as follows.

List<Integer> lista = new ArrayList<Integer>();
lista.add(new Integer(30));
lista.add(new Integer(25));
lista.add(new Integer(20));
Integer oject = new Integer(25);

int index = lista.indexOf(oject);
if (index > -1)
{
   System.out.println(lista.get(index));
}

In my case my object is Integer as you have it, but you can make an object as needed and it will be the same formula. lista.indexOf(oject) , so you dynamically grab any position.

    
27.09.2016 / 02:28