extract element to arraylist and pass it to string

0

As you will know, extracting an item from an ArrayList is done in the following way:

arraylist.get(position).get("elemento")

to convert it to String as I read:

String abc =TextUtils.join("", ArrayList);

How to make a specific item and convert it to String? example:

 String abc =TextUtils.join("", ArrayList.get(position).get(elemento));

try that way but it does not work.

    
asked by Yoel Mendoza 01.10.2017 в 22:22
source

3 answers

0

To pass an Element from an ArrayList to String you just have to do the following

String cadena = tuLista.get(indice).toString();

Add more information about what items your list has to guide you better, Regards

    
answered by 01.10.2017 / 23:26
source
0

Depends on how your ArrayList is:

  • If it is made up of String objects or objects that wrap primitive data: Integer , Long , etc, you do not need to use toString to display them. You can show them as you would show any variable of its type. That means you can read it using the method get(indice)

  • If it is constituted by objects, then you can read it by adding the get(indice) method toString() or a partial method, in case you want to obtain a specific data of the array, for example getName() in the case of class People .

Keep in mind that the first element always has the index 0 and so on.

Let's see an example with code:

VIEW DEMO

import java.util.*;
import java.lang.*;

class Rextester
{  
    public static void main(String args[])
    {

        ArrayList<Integer> arrListInt = new ArrayList<Integer>(3);
        ArrayList<String> arrListString = new ArrayList<String>(3);
        ArrayList<People> arrListPeople = new ArrayList<People>(3);


        arrListInt.add(15);
        arrListInt.add(22);
        arrListInt.add(33);

        arrListString.add("Uno");
        arrListString.add("Dos");
        arrListString.add("Tres");

        arrListPeople.add(new People("Pedro",28));
        arrListPeople.add(new People("Santiago",30));
        arrListPeople.add(new People("Juan",22));

        System.out.println("Primer elemento de arrListInt: " + arrListInt.get(0));    
        System.out.println("Segundo elemento de arrListString: = " + arrListString.get(1));   

        System.out.println("Tercera Persona de arrListPeople: = " + arrListPeople.get(2).toString());     
        System.out.println("Segunda Persona (Nombre) de arrListPeople: = " + arrListPeople.get(1).getName());     

   }


}


class People 
{
    String name;
    int age;

    People (String name,int age) 
    {
        this.name=name;
        this.age=age;
    }

    String getName() 
    {
        return name;
    }

    int getAge() 
    {
        return age;
    }

    @Override
    public String toString() 
    {
        return "Nombre: "+getName()+" - Edad:"+getAge();
      }

}

Resultado:

Primer elemento de arrListInt: 15
Segundo elemento de arrListString: = Dos
Tercera Persona de arrListPeople: = Nombre: Juan - Edad:22
Segunda Persona (Nombre) de arrListPeople: = Santiago
    
answered by 02.10.2017 в 01:07
-1

Very simple

I understand that it is a two-dimensional array

    String elemento= nombreDelarray.get(positionPadre).get(positionHijo).toString();
    
answered by 02.10.2017 в 23:22