0

I tried putting an array into the array list

ArrayList <DNA> matingppool;

The array is 0 and 1

when I try to show it in logcat in a way

population [letsmat [i]].getBiner ()

the result is

[I @ 25a0766d

but when I display it logcat in a way

Arrays.toString (population[letsmat[i]].getBiner ())

logcat displays

[0, 0, 0, 1]

the question is

which is the best way to enter an array into the pooling arraylist so that arraylist returns the array value again?

matingppool.add (population [letsmat [i]]);

or this one

matingppool.add (Arrays.toString (population [letsmat [i]]. getBiner ()));

Does " [I @ 25a0766d " represent an array?

4 Answers4

1

there are 2 ways to add arrays in to arraylist, -> first one using arrays.asList()

ArrayList<String> arrayList=new ArrayList<>();
String[] data=new String[]{"ABC","DEF","GHI"};

arrayList.addAll(Arrays.asList(data));

-> second one is using Colliections,

    ArrayList<String> arrayList=new ArrayList<>();
    String[] data=new String[]{"ABC","DEF","GHI"};
    Collections.addAll(arrayList, data);

this will work for you.

Jay Mungara
  • 6,663
  • 2
  • 27
  • 49
1

To understand what does this present: [I @ 25a0766d, go to below link https://stackoverflow.com/a/8410311/1664430

You can use Arrays.asList() and addAll method of List to achieve this concisely. Below is a sample code

public static void main(String[] args) {
    List<String> lst = new ArrayList<>();
    String[] strLst = { "1,", "2", "3" };
    lst.addAll(Arrays.asList(strLst));
    System.out.println(lst);
}

And if your purpose it to print/iterate over the Array, you can use lambda expression also

Arrays.asList(strLst).stream().forEach(System.out::print);
Ashishkumar Singh
  • 3,580
  • 1
  • 23
  • 41
-1

Please go through the example below.
Override toString() method to print Class object in system out.
Comment if something is not clear.

public static void main(String[] args) {

    //Part1: Add toString() Method in DNA class
    ArrayList<DNA> matingPool = new ArrayList<>();
    matingPool.add(new DNA());
    matingPool.add(new DNA());
    matingPool.add(new DNA());
    System.out.println(matingPool);

    //Part2: use AddAll to add all the objects in one go.
    ArrayList<DNA> newMatingPool = new ArrayList<>(); // You can do new ArrayList<>(matingPool) as well.
    newMatingPool.addAll(matingPool);
    System.out.println(newMatingPool);

}

static class DNA {
    int a;
    String b;

    public DNA() {
        this.a = 10;
        this.b = "BB";
    }

    @Override
    public String toString() {
        return "DNA{" +
                "a=" + a +
                ", b='" + b + '\'' +
                '}';
    }
}
impossible
  • 2,380
  • 8
  • 36
  • 60
-1

Ok as my understanding of your question, Lets say there is a class User

public class User {

private String userId;
private String userName;
private String userEmail;

public String getUserId() {
    return userId;
}

public void setUserId(String userId) {
    this.userId = userId;
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getUserEmail() {
    return userEmail;
}

public void setUserEmail(String userEmail) {
    this.userEmail = userEmail;
}

public String toString() {
    return userId + " " + userName + " " + userEmail;
}
}

If we create a ArrayList of Class User and prints it using

ArrayList<User> users = new ArrayList<>();
//Add Items in ArrayList 

For printing use code

for(Users user : users){
Log.d("TAG",user.toString);
}

It will print the values in User ArrayList.Dont forget to add toString() method in your Model class

Hope it helps you.

Yogesh Katkar
  • 369
  • 1
  • 4
  • 16