如何在Java中返回一个已创建的数组?

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

How do I return a created Array in Java?

问题

import java.util.Arrays;

public class HelloWorld {

    public static void main(String []args) {
        String str = "ABBACCCCAC";
        int[] arr = new int[3]; // Create an array to store counts of 'A', 'B', and 'C'
        int acount = 0, bcount = 0, ccount = 0;

        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == 'A') {
                acount++;
            } else if (str.charAt(i) == 'B') {
                bcount++;
            } else if (str.charAt(i) == 'C') {
                ccount++;
            }
        }
        
        // Store the counts in the array
        arr[0] = acount;
        arr[1] = bcount;
        arr[2] = ccount;

        System.out.print(Arrays.toString(arr));
    }
}
英文:

In this program, I'm supposed to count the amount of times the letters 'A','B' and 'C' are in the string and return that as an array. The input is ABBACCCCAC and I'm supposed to get an output of [3, 2, 5] but I'm getting [1, 1, 2, 2, 1, 2, 3, 4, 3, 5]

import java.util.Arrays;
public class HelloWorld{

 public static void main(String []args){
    String str = &quot;ABBACCCCAC&quot;;
    int[] arr = new int[str.length()];
    int acount = 0, bcount = 0, ccount = 0;
    
    for(int i =0; i&lt;str.length();i++){
        if(str.charAt(i) == &#39;A&#39;){
            acount++;
            arr[i] = acount;
        }
        else if(str.charAt(i) == &#39;B&#39;){
            bcount++;
            arr[i] = bcount;
        }
        else if(str.charAt(i) == &#39;C&#39;){
            ccount++;
            arr[i] = ccount;
        }
    }
    System.out.print(Arrays.toString(arr));
 }
}

答案1

得分: 2

你离正确很近。只需要将数组大小更改为3,并将索引更改为0、1和2。

   String str = "ABBACCCCAC";
    int[] arr = new int[3];
    int acount = 0, bcount = 0, ccount = 0;
    
    for(int i =0; i<str.length();i++){
        if(str.charAt(i) == 'A'){
            acount++;
            arr[0] = acount;
        }
        else if(str.charAt(i) == 'B'){
            bcount++;
            arr[1] = bcount;
        }
        else if(str.charAt(i) == 'C'){
            ccount++;
            arr[2] = ccount;
        }
    }
    System.out.print(Arrays.toString(arr));

你也可以直接递增数组位置。

arr[0]++; // 就像这样。
英文:

You were close. Just change your array size to 3 and the indiced to 0,1, and 2.

   String str = &quot;ABBACCCCAC&quot;;
    int[] arr = new int[3];
    int acount = 0, bcount = 0, ccount = 0;
    
    for(int i =0; i&lt;str.length();i++){
        if(str.charAt(i) == &#39;A&#39;){
            acount++;
            arr[0] = acount;
        }
        else if(str.charAt(i) == &#39;B&#39;){
            bcount++;
            arr[1] = bcount;
        }
        else if(str.charAt(i) == &#39;C&#39;){
            ccount++;
            arr[2] = ccount;
        }
    }
    System.out.print(Arrays.toString(arr));

Your could also just increment the array locations directly.

arr[0]++; // like this.

答案2

得分: 0

现在,在每次计数的步骤中,您都在将计数分配给数组,而您应该在计数结束时执行此操作。此外,请注意,您创建了一个与字符串长度相同的数组,而不是预期的3

您只需要将数组的创建移到末尾,使用数组创建表达式:

String str = "ABBACCCCAC";
int acount = 0, bcount = 0, ccount = 0;

for(int i = 0; i < str.length(); i++) {
    if(str.charAt(i) == 'A'){
        acount++;
    }
    else if(str.charAt(i) == 'B'){
        bcount++;
    }
    else if(str.charAt(i) == 'C'){
        ccount++;
    }
}

int[] arr = new int[] { acount, bcount, ccount }; // <--- 在这里!
System.out.print(Arrays.toString(arr));
英文:

Right now, you are assigning the counts to the array at each step of the counting, whereas you should have done this at the end of the count. Also, note that you created an array the same length of the string, rather than the expected 3.

You should just move the creation of the array to the end, with an array creation expression:

String str = &quot;ABBACCCCAC&quot;;
int acount = 0, bcount = 0, ccount = 0;

for(int i =0; i&lt;str.length();i++){
    if(str.charAt(i) == &#39;A&#39;){
        acount++;
    }
    else if(str.charAt(i) == &#39;B&#39;){
        bcount++;
    }
    else if(str.charAt(i) == &#39;C&#39;){
        ccount++;
    }
}

int[] arr = new int[] { acount, bcount, ccount }; // &lt;--- here!
System.out.print(Arrays.toString(arr));

huangapple
  • 本文由 发表于 2020年7月24日 07:51:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63064771.html
匿名

发表评论

匿名网友

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

确定