Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中的字符串字符下打印星号(*)_Java_String_Collections_Hashmap - Fatal编程技术网

在java中的字符串字符下打印星号(*)

在java中的字符串字符下打印星号(*),java,string,collections,hashmap,Java,String,Collections,Hashmap,我在一次采访中被问到一个java问题。在字符串中打印不同的字符,并在每个字符下打印星号(*),以显示该字符在该字符串中重复的次数 例如:我的字符串是“GOOGLE”,那么输出应该是 G O L E * * * * * * 我尝试了java,我能够创建一个HashMap,它将字符和重复次数存储在字符串中。但是HashMap不是基于字符串的插入顺序。我也不知道下一步该怎么做。有人能帮我吗?提前谢谢 public void myFunction(String str) { int lengt

我在一次采访中被问到一个java问题。在字符串中打印不同的字符,并在每个字符下打印星号(*),以显示该字符在该字符串中重复的次数

例如:我的字符串是“GOOGLE”,那么输出应该是

G O L E
* * * *
* *
我尝试了java,我能够创建一个HashMap,它将字符和重复次数存储在字符串中。但是HashMap不是基于字符串的插入顺序。我也不知道下一步该怎么做。有人能帮我吗?提前谢谢

public void myFunction(String str) {
    int length = str.length();
    HashMap<Character, Integer> hm = new HashMap<>();
    for(int i=0;i<length;i++){
        char ch = str.charAt(i);
        if(hm.containsKey(ch)){
            hm.put(ch, hm.get(ch)+1);               
        }
        else {
            hm.put(ch, 1);
        }


    }
        System.out.println(hm);
}

OUTPUT - Enter a String: 
GOOGLE
{E=1, G=2, L=1, O=2}
public void myFunction(String str){
int length=str.length();
HashMap hm=新的HashMap();

对于(int i=0;i如果您使用
LinkedHashMap
,它将保持插入顺序。您可以这样做。还可以添加
max
变量,因为我们以后在打印时需要它

String input = "GOOGLE";
int max = 0;
LinkedHashMap<Character, Integer> map = new LinkedHashMap<>();
for (char c: input.toCharArray()){
    if (map.containsKey(c)){
        map.put(c, map.get(c) + 1);
    }else{
        map.put(c, 1);
    }
    max = Math.max(max, map.get(c));
}
System.out.println(map);
然后迭代你需要打印多少行,然后迭代每个字符。类似这样的事情应该可以做到

for (int i=0; i<=max; i++){
    for (char c: map.keySet()){
        if (i==0){
            System.out.print(c);
        }else if (i<= map.get(c)){
            System.out.print("*");
        }else{
            System.out.print(" ");
        }
    }
    System.out.println();
}
这是一个好的开始

接下来我要做的是将
HashMap
更改为
LinkedHashMap
,这样我们可以保持字符的顺序,并添加
long
,以了解字符出现的最大次数。因此,我会将当前代码更改为:

public void myFunction(String str) {
int length = str.length();
long maxOcurrences = 0;
LinkedHashMap<Character, Integer> hm = new LinkedHashMap<>();
for(int i=0;i<length;i++){
    char ch = str.charAt(i);
    long nextValue;
    if(hm.containsKey(ch)){
        nextValue = hm.get(ch)+1
        hm.put(ch, nextValue);               
    }
    else {
        nextValue = 1;
        hm.put(ch, nextValue);
    }

    if(nextValue > maxOcurrences)
    {maxOcurrences = nextValue;}


}
    System.out.println(hm);
}
for (Map.Entry<Character, Integer> entry : hm.entrySet()) {
    System.out.print(entry.getKey());
}
System.out.println();
最后,我将创建一个循环,该循环迭代
maxOcurrences
次,并在需要时打印
*

for(int i = 0; i < maxOcurrences; i++)
{
    //Iterate over each character again
    for (Map.Entry<Character, Integer> entry : hm.entrySet()) {
        if(entry.getValue() > i)
        {
            //Print Star
            System.out.print("*");
        }else{
            //Print space
            System.out.print(" ");
        }
        System.out.println();
    }
}
for(int i=0;ii)
{
//印刷明星
系统输出打印(“*”);
}否则{
//打印空间
系统输出打印(“”);
}
System.out.println();
}
}

你可以在这里找到答案:特别是LinkedHashMap。像这样简单的事情不需要使用HashMap。它是相关的,但有点过分。一个简单的1D数组,有3行代码就可以了。在遍历键时,你只需捕捉到第一行的情况。这样你只需遍历映射一次。@gonzo感谢您的输入!这是真的,但是差别并没有那么大,
if
s往往比没有
if
s的额外循环的cpu成本更高。
for (Map.Entry<Character, Integer> entry : hm.entrySet()) {
    System.out.print(entry.getKey());
}
System.out.println();
for(int i = 0; i < maxOcurrences; i++)
{
    //Iterate over each character again
    for (Map.Entry<Character, Integer> entry : hm.entrySet()) {
        if(entry.getValue() > i)
        {
            //Print Star
            System.out.print("*");
        }else{
            //Print space
            System.out.print(" ");
        }
        System.out.println();
    }
}