如何在Java中打印char []?

时间:2019-02-28 02:15:14

标签: java arrays printing char

当我尝试打印char数组时,我只会得到一堆盒子[] [] []。

这是我的代码。我该如何打印并获取数字值数组,例如[1,2,3]?

package Com;

import java.util.*;

public class Solution {
    public static void main(String[] args) {
        Solution l1 = new Solution();

//        int[] x = {3,3,7,7,10,10,11};

        String[] x = {"eat", "tea", "tan", "ate", "nat", "bat"};

        l1.groupAnagrams(x);
    };

    public List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> ans = new ArrayList<>();
        Map<Integer,List<String>> m = new HashMap<>();

        for(String s: strs) {
            char[] chars = new char[26];
            for (char c : s.toCharArray()) {
                chars[c-'a']++;
            }
            System.out.println(chars);
        }

            return ans;
        };
    };

1 个答案:

答案 0 :(得分:3)

您应该创建int而不是char的数组。该数组包含字符 counts ,而不是字符。为了清楚起见,让我们将其重命名为counts

int[] counts = new int[26];
for (char c : s.toCharArray()) {
    counts[c-'a']++;
}
System.out.println(Arrays.toString(counts));

然后得到一个可读的字符串调用Arrays.toString()