When I try to print my char array I just get a bunch of boxes [][][].
Here is my code. How do I print this and get the number value array such as [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;
};
};
You should be creating arrays of int
not char
. The array holds character counts , not characters. For clarity let's rename it to counts
.
int[] counts = new int[26];
for (char c : s.toCharArray()) {
counts[c-'a']++;
}
System.out.println(Arrays.toString(counts));
Then to get a readable string call Arrays.toString()
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.