需要帮助解决 Java 数组问题

发布于 2024-10-21 11:20:41 字数 402 浏览 2 评论 0原文

我需要一点帮助。我正在尝试编写一个小型 Java 程序,该程序创建并返回一个包含 a 的所有元素的数组,其中每个元素都是重复的。但是,每次我尝试执行代码时,都会出现“越界”异常。请告诉我我哪里出错了。谢谢!这是我到目前为止所拥有的:

public class integerStutter
{
    public static int[] stutter(int [] a){
       int[] j = new int[a.length*2];

        for(int i=0; i < j.length; i++){

            j[i] = a[i];
            j[i+1] = a[i];
            i++;
    }
    return j;
   }
}

I need a little help. I'm trying to put together a small Java program that creates and returns an array containing all elements of a, each of these duplicated. However, every time I try to execute my code I get an "out of bounds" exception. Please give me an idea of where I'm going wrong. Thanks! Here is what I have so far:

public class integerStutter
{
    public static int[] stutter(int [] a){
       int[] j = new int[a.length*2];

        for(int i=0; i < j.length; i++){

            j[i] = a[i];
            j[i+1] = a[i];
            i++;
    }
    return j;
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

耀眼的星火 2024-10-28 11:20:41

新旧数组的大小不相等。您尝试使用新数组(双倍大小)的有效索引来访问旧数组中的元素,这导致了异常。

相反尝试:

// iterate over the old array.
for(int i=0; i < a.length; i++){

        // index into new array.
        int x = 2 * i;

        // copy old array ele at index i into new array at index x and x+1.
        j[x] = j[x+1] = a[i];        
} 

The old and new array are not of equal sizes. You are trying to access elements from old array using valid indices for the new array (which is of double size) and this is causing the exception.

Instead try:

// iterate over the old array.
for(int i=0; i < a.length; i++){

        // index into new array.
        int x = 2 * i;

        // copy old array ele at index i into new array at index x and x+1.
        j[x] = j[x+1] = a[i];        
} 
烂人 2024-10-28 11:20:41
int[] j = new int[a.length*2];

因此,数组ja的大小不相等。但循环运行直到j.length -

for(int i=0; i < j.length; i++){

     j[i] = a[i];    // array out of bounds once i passes a.length
     j[i+1] = a[i];  // array out of bounds once i passes a.length
     i++;  // Why again incrementing here ?
}
int[] j = new int[a.length*2];

So, the size of arrays j, a are not equal. But the loop runs until j.length -

for(int i=0; i < j.length; i++){

     j[i] = a[i];    // array out of bounds once i passes a.length
     j[i+1] = a[i];  // array out of bounds once i passes a.length
     i++;  // Why again incrementing here ?
}
攀登最高峰 2024-10-28 11:20:41

你可以这样做

for(int i=0,k=0;i<a.length;i++,k=k+2)
{
     j[k]=j[k+1]=a[i];

}

you can do it like this

for(int i=0,k=0;i<a.length;i++,k=k+2)
{
     j[k]=j[k+1]=a[i];

}
夏九 2024-10-28 11:20:41

如果我正确理解了这个问题,这将满足您的需要。这也是一个很好的清洁解决方案。

public static int[] stutter(int[] a) {
    int[] j = new int[a.length * 2];

    for (int i = 0; i < a.length; i++) {
        j[i * 2] = a[i];
        j[i * 2 + 1] = a[i];
    }

    return j;
}

If I understand the question correctly, this will do what you need. It is also a nice clean solution.

public static int[] stutter(int[] a) {
    int[] j = new int[a.length * 2];

    for (int i = 0; i < a.length; i++) {
        j[i * 2] = a[i];
        j[i * 2 + 1] = a[i];
    }

    return j;
}
甜宝宝 2024-10-28 11:20:41
public class integerStutter
{
public static int[] stutter(int [] a){
    int[] j = new int[a.length*2];

    for(int i=0; i < j.length; i++){

        j[i] = a[i];
        j[i+1] = a[i];
        i++;
    }
    return j;
}
}
public class integerStutter
{
public static int[] stutter(int [] a){
    int[] j = new int[a.length*2];

    for(int i=0; i < j.length; i++){

        j[i] = a[i];
        j[i+1] = a[i];
        i++;
    }
    return j;
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文