0

I am working on the first part of a String permutation problem and I am just looping over the first char of a string and swap it with every following char of that same String. I initialized an empty ArrayList to store all of those permutations called listeFinale. When I am printing that ArrayList, I am getting a collection of object and not values ([[C@61bbe9ba, [C@61bbe9ba, [C@61bbe9ba, [C@61bbe9ba]), how can I print each char stored in the ArrayList?

import java.util.ArrayList;
import java.util.List;

public class checkPermu {

  public static void main(String[] args) {
    String myString = "aabc";
    applyPermu(myString);
  }

  public static void applyPermu(String toCheck){
    char[] newString = toCheck.toCharArray();
    List listeFinale = new ArrayList();
    for(int i = 0 ; i < newString.length ; i ++){
      char temp = newString[0];
      newString[0] = newString[i];
      newString[i] = temp;
      listeFinale.add(newString);
      System.out.println(listeFinale);
    }
  }
}
  • 3
    You aren't storing characters in the your `ArrayList`, you're storing arrays of characters. Do a quick search on `Arrays.toString()`. – azurefrog Sep 26 '19 at 14:52

1 Answers1

3

First of all, don't use raw types for your List please.. Change:

List listeFinale = new ArrayList();

to:

List<char[]> listeFinale = new ArrayList<>();

As for your actual problem. Those values you see are the default toString() outputs of your inner character-arrays. You could iterate over your list, and call the java.util.Arrays.toString(char[]) method for them like this:

listeFinale.forEach(arr -> System.out.println(Arrays.toString(arr)));

Or, if you want to print them back as String again, use new String(char[]):

listeFinale.forEach(arr -> System.out.println(new String(arr)));

Try it online.

Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135