Index X out of bounds for length X 索引 X 超出长度 X 的范围

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

Index X out of bounds for length X

问题

I'm learning Java and while learning methods I came across an error. What's more, code does what I want, (it displays stars), but it also throws an error. I don't quite know why.

Here is code and error below:

import java.util.Scanner;

public class TaskMethods {
    
    //Display the stars
    public static void main(String[] args) {
        System.out.println("Insert number of stars:");

        //declare numbers of stars
        int num = getInt();

        //Display the stars by 'stars' method
        System.out.println(stars(num));
    }

    //method for input int variables
    public static int getInt() {
        return new Scanner(System.in).nextInt();
    }

    //method for inputting stars to an array and also for displaying them.
    public static String stars(int num) {
        String[] star = new String[num];
        for (int i = 0; i < star.length; i++) {
            star[i] = "*";
            System.out.print(star[i]);
        }
        return star[num - 1];
    }
}

I tried to limit the loop by the array length (like always) and It's nothing. There is no line where we declare values outside the range of the array. In every post I've seen the problem was wrong range.

That's why I'm looking for help here.

英文:

I'm learning Java and while learning methods I came across an error. What's more, code does what I want, (it displays stars), but it also throws an error. I don't quite know why.

Here is code and error below:

import java.util.Scanner;

public class TaskMethods {
    
    //Display the stars
    public static void main (String[] args){
        System.out.println(&quot;Insert number of stars:&quot;);

    //declare numbers of stars
        int num = getInt();

    //Display the stars by &#39;stars&#39; method
        System.out.println(stars(num));
    }
    //method for input int variables
    public static int getInt(){
        return new Scanner(System.in).nextInt();
    }

    //method for inputing stars to array and also for displaying them.
    public static String stars(int num){
        String[] star = new String[num];
        for (int i = 0; i &lt; star.length; i++){
            star[i] = &quot;*&quot;;
            System.out.print(star[i]);
        }
        return star[num];
    }
}

enter image description here

I tried to limit the loop by the array lenght (like always) and It's nothing.
There is no line where we declare values ​​outside the range of the array.
In every post I've seen the problem was wrong range.

Thats why I'm looking for help here.

答案1

得分: 0

你的数组长度为3。你会在star[0]、star[1]和star[2]的位置有星星。
在stars方法中,你返回了star[3],但这个位置不存在。

英文:

Say your array is 3 of length. You will have stars at star[0], star[1], and star[2].
In the stars method, you return star[3] and this doesn't exist.

答案2

得分: 0

以下是您要翻译的部分:

你正在尝试访问_star[num]_,这将在最后一个索引值之外为1。

return star[num];

此外,您正在将_stars_方法的返回值提供给_println_调用,如下所示。

//通过“stars”方法显示星号
System.out.println(stars(num));

这是不必要的,因为_stars_使用_print_来显示值。

您可以采取两种方法之一。

一种方法是只调用_stars(num),而不调用_println。这也允许您将_stars_的返回类型更改为_void_。

//用于输入星号到数组并显示它们的方法。
public static void stars(int num){
    String[] star = new String[num];
    for (int i = 0; i &lt; star.length; i++){
        star[i] = &quot;*&quot;;
        System.out.print(star[i]);
    }
}

或者,您可以在_stars_内部填充一个_String_值,然后返回它。

public static String stars(int num){
    String[] star = new String[num];
    String string = &quot;&quot;;
    for (int i = 0; i &lt; star.length; i++){
        star[i] = &quot;*&quot;;
        string += star[i];
    }
    return string;
}

随后,您可以在_for-loop_内减少_string_和_star[i]_的赋值。

string += star[i] = &quot;*&quot;;

并且,由于我们正在讨论这个话题,您可以利用_Arrays#fill方法来填充一个数组,并使用String#join_方法来连接这些值。

public static String stars(int num){
    String[] star = new String[num];
    Arrays.fill(star, &quot;*&quot;);
    return String.join(&quot;&quot;, star);
}

此外,_String#repeat_方法可以生成一系列重复的字符。

public static String stars(int num){
    return &quot;*&quot;.repeat(num);
}

以下是一个示例输入和输出。

插入星号数量:
5
*****
英文:

You're attempting to access star[num], which would be 1 outside the last indexed value.

return star[num];

Additionally, you are supplying the return value of the stars method, to a println call, here.

//Display the stars by &#39;stars&#39; method
System.out.println(stars(num));

Which, isn't necessary, since stars uses print to display the value.

There are two approaches you can take.

One, simply call stars(num), without the println call.
This would also allow you to remove the return type of stars&mdash;by changing it to void.

//method for inputing stars to array and also for displaying them.
public static void stars(int num){
    String[] star = new String[num];
    for (int i = 0; i &lt; star.length; i++){
        star[i] = &quot;*&quot;;
        System.out.print(star[i]);
    }
}

Or, you can populate a String value within stars, and return that.

public static String stars(int num){
    String[] star = new String[num];
    String string = &quot;&quot;;
    for (int i = 0; i &lt; star.length; i++){
        star[i] = &quot;*&quot;;
        string += star[i];
    }
    return string;
}

Subsequently, you can reduce the string and star[i] assignment, within the for-loop.

string += star[i] = &quot;*&quot;;

And, since we're on the topic, you can utilize the Arrays#fill method, to populate an array, and the String#join method to concatenate the values.

public static String stars(int num){
    String[] star = new String[num];
    Arrays.fill(star, &quot;*&quot;);
    return String.join(&quot;&quot;, star);
}

Furthermore, the String#repeat method, can produce a sequence of repeating characters.

public static String stars(int num){
    return &quot;*&quot;.repeat(num);
}

Here is an example input and output.

Insert number of stars:
5
*****

huangapple
  • 本文由 发表于 2023年6月12日 17:59:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76455526.html
匿名

发表评论

匿名网友

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

确定