如何找出特定名称重复了多少次?

huangapple go评论107阅读模式
英文:

How to find a particular name repeated how many number of times?

问题

我遇到了输出问题,有人能帮我吗?
这是一个用于查找特定名称重复了多少次的代码。

  1. package example1;
  2. import java.util.Scanner;
  3. public class example1 {
  4. public static void main(String[] args) {
  5. // TODO Auto-generated method stub
  6. int i,j,t,c;
  7. String a[]=new String[10];
  8. String b[]=new String[10];
  9. Scanner sc=new Scanner(System.in);
  10. for(i=0;i<10;i++)
  11. {
  12. a[i]=sc.nextLine();
  13. b[i]=a[i];
  14. }
  15. for(i=0;i<10;i++)
  16. {
  17. c=0;
  18. for(j=i+1;j<9;j++)
  19. {
  20. if(b[j]==b[i])
  21. {
  22. c++;
  23. for(t=j;t<10;t++)
  24. {
  25. b[t]=b[t+1];
  26. }
  27. }
  28. }
  29. System.out.print(b[i]+" 被重复了 "+ c +" 次 ");
  30. }
  31. }
  32. }
英文:

I'm recieving issue with the output can anyone help me with it?
It is a code for finding how many times a particular name has been repeated.

  1. package example1;
  2. import java.util.Scanner;
  3. public class example1 {
  4. public static void main(String[] args) {
  5. // TODO Auto-generated method stub
  6. int i,j,t,c;
  7. String a[]=new String[10];
  8. String b[]=new String[10];
  9. Scanner sc=new Scanner(System.in);
  10. for(i=0;i&lt;10;i++)
  11. {
  12. a[i]=sc.nextLine();
  13. b[i]=a[i];
  14. }
  15. for(i=0;i&lt;10;i++)
  16. {
  17. c=0;
  18. for(j=i+1;j&lt;9;j++)
  19. {
  20. if(b[j]==b[i])
  21. {
  22. c++;
  23. for(t=j;t&lt;10;t++)
  24. {
  25. b[t]=b[t+1];
  26. }
  27. }
  28. }
  29. System.out.print(b[i]+&quot; is repeated &quot;+ c +&quot; times &quot;);
  30. }
  31. }
  32. }

答案1

得分: 1

试试这个。

  1. String[] arr = new String[] {"a", "b", "c", "d", "a", "b", "c", "a", "b", "a"};
  2. List<String> list = Arrays.asList(arr);
  3. Set<String> set = new HashSet<>(list);
  4. for (String name : set)
  5. System.out.println(name + " 重复了 " + Collections.frequency(list, name) + " 次");

如果你希望比较时不区分大小写,可以试试这个。

  1. String[] arr = new String[] {"A", "B", "c", "D", "a", "b", "c", "a", "b", "a"};
  2. List<String> list = new ArrayList<>(10);
  3. for (String name : arr)
  4. list.add(name.toLowerCase());
  5. Set<String> set = new HashSet<>(list);
  6. for (String name : set)
  7. System.out.println(name + " 重复了 " + Collections.frequency(list, name) + " 次");

但会需要更多的内存空间。

英文:

Try this.

  1. String[] arr = new String[] {&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;d&quot;,&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;a&quot;,&quot;b&quot;,&quot;a&quot;};
  2. List&lt;String&gt; list = Arrays.asList(arr);
  3. Set&lt;String&gt; set = new HashSet&lt;&gt;(list);
  4. for(String name : set)
  5. System.out.println(name + &quot; is repeated &quot; + Collections.frequency(list, name) + &quot; times&quot;);

If you want your comparison to be case insensitive, then try this.

  1. String[] arr = new String[] {&quot;A&quot;,&quot;B&quot;,&quot;c&quot;,&quot;D&quot;,&quot;a&quot;,&quot;b&quot;,&quot;c&quot;,&quot;a&quot;,&quot;b&quot;,&quot;a&quot;};
  2. List&lt;String&gt; list = new ArrayList&lt;&gt;(10);
  3. for(String name : arr)
  4. list.add(name.toLowerCase());
  5. Set&lt;String&gt; set = new HashSet&lt;&gt;(list);
  6. for(String name : set)
  7. System.out.println(name + &quot; is repeated &quot; + Collections.frequency(list, name) + &quot; times&quot;);

Will require more space though.

答案2

得分: 0

以下代码应有助于确定重复项:

  1. public class Repetition {
  2. public static void main(String[] args) {
  3. String string = "Sam sam is good boy james is sams friend clyra is clyra";
  4. int count;
  5. string = string.toLowerCase();
  6. String words[] = string.split(" ");
  7. System.out.println("重复的单词:");
  8. for(int i = 0; i < words.length; i++) {
  9. count = 1;
  10. for(int j = i+1; j < words.length; j++) {
  11. if(words[i].equals(words[j])) {
  12. count++;
  13. words[j] = "0"; // 为了以后的参考将其置为0
  14. }
  15. }
  16. if(count > 1 && !words[i].equals("0"))
  17. System.out.println(words[i]);
  18. }
  19. }
  20. }
英文:

The following code should help for determining the repetition

  1. public class Repetation {
  2. public static void main(String[] args) {
  3. String string = &quot;Sam sam is good boy james is sams friend clyra is clyra&quot;;
  4. int count;
  5. string = string.toLowerCase();
  6. String words[] = string.split(&quot; &quot;);
  7. System.out.println(&quot;Duplicate words: &quot;);
  8. for(int i = 0; i &lt; words.length; i++) {
  9. count = 1;
  10. for(int j = i+1; j &lt; words.length; j++) {
  11. if(words[i].equals(words[j])) {
  12. count++;
  13. words[j] = &quot;0&quot;;//making it 0 for future reference
  14. }
  15. }
  16. if(count &gt; 1 &amp;&amp; words[i] != &quot;0&quot;)
  17. System.out.println(words[i]);
  18. }
  19. }
  20. }

答案3

得分: 0

  1. public static int countWord(String input, String text) {
  2. int index = input.indexOf(text);
  3. if (index == -1 || text.isEmpty()) {
  4. return 0;
  5. }
  6. int count = 0;
  7. do {
  8. count++;
  9. index = input.indexOf(text, index + text.length());
  10. } while (index > 0 && index < input.length());
  11. return count;
  12. }

This code will return 4:

  1. int total = countWord("abcs8abc88habcabci7h", "abc");
英文:
  1. public static int countWord(String input, String text) {
  2. int index = input.indexOf(text);
  3. if (index == -1 || text.isEmpty()) {
  4. return 0;
  5. }
  6. int count = 0;
  7. do {
  8. count++;
  9. index = input.indexOf(text, index + text.length());
  10. } while (index &gt; 0 &amp;&amp; index &lt; input.length());
  11. return count;
  12. }

This code will return 4:

  1. int total = countWord(&quot;abcs8abc88habcabci7h&quot;, &quot;abc&quot;);

huangapple
  • 本文由 发表于 2020年9月11日 12:24:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63840751.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定