Java数组出界问题 [英] Java array out of bound issue

查看:69
本文介绍了Java数组出界问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java代码,其中我从2维数组创建2个数组列表..但我得到一个数组Out Of Bound异常..我将2维数组传递给我的类方法答案..但它不工作..



I have a code for Java in which I am creating 2 Array List from 2 dimensional array.. But I am getting an Array Out Of Bound exception.. I am passing 2 dimensional array to my class method answer.. But it is not working..

import java.io.*;
import java.util.*;

public class Abc {
    static void answer(Integer[][] a,int l){
        Integer[] un = new Integer[l];
        Integer[] un1 = new Integer[l];
        final Integer z = 0;
        int max1=0,max2=0,count1=0,count2=0;
        int ind=0;
        ArrayList<integer> arr1 = new ArrayList<integer>(Arrays.asList(a[0][l])); // Line 14
        ArrayList<integer> arr2 = new ArrayList<integer>(Arrays.asList(a[1][l]));

        for(int i=0;i<l;i++){
            if(i % 2 == 0){
                max1 = Collections.max(arr1);
                count1 += max1;
                ind = arr1.indexOf(max1);
                arr1.set(ind,z);
                arr2.set(ind,z);
            }
            else{
                max2 = Collections.max(arr2);
                count2 += max2;
                ind = arr2.indexOf(max2);
                arr2.set(ind,0);
                arr1.set(ind,0);
            }

        }

        if(count1 < count2){
            System.out.println("Second");
        }
        else{
            if(count1 > count2){
                System.out.println("First");
            }
            else{
                System.out.println("Tie");
            }

        }

    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int times = sc.nextInt();
        for(int i=0;i<times;i++){
            int val = sc.nextInt();
            Integer[][] arr = new Integer[2][val];
            for(int k=0;k<2;k++){
                for(int j=0;j<val;j++){
                arr[k][j] = sc.nextInt();
                }
            }

             Abc.answer(arr,val);

        }

    }
}





错误:线程main中的异常java.lang.ArrayIndexOutOfBoundsException:3

at Abc.answer(Abc.java:14)

at Abc.main(Abc.java :80)



提前致谢!



我尝试过:



我将2维数组从main方法传递给ans方法..但是我得到了ArrayOutOfBound Exception ..



Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Abc.answer(Abc.java:14)
at Abc.main(Abc.java:80)

Thanks In Advance!

What I have tried:

I am passing 2 dimensional array from main method to ans method.. But I am getting ArrayOutOfBound Exception..

推荐答案

a[0][l]



如果 a 在其第二维中有 l 元素,则这些元素将从零索引到 l - 1 。因此, a [0] [l] 将抛出异常。

如果要访问最后一个元素,则应使用 a [0] [l - 1] 而不是。



  • 第一维中的元素是索引从零到 l - 1
  • 第二维中的元素从零索引到 l - 1

  • If a has l elements in its second dimension, then these elements are indexed from zero to l - 1. Thus, a[0][l] will throw an exception.
    If you want to access the last element, you should use a[0][l - 1] instead.


    • The elements in the first dimension are indexed from zero to l - 1.
    • The elements in the second dimension are indexed from zero to l - 1.

    • Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3



      此错误消息表示您的代码尝试访问不存在的数组元素。

      Java基于零,这意味着一个大小的数组3有0,1和2的元素。


      This error message means that your code try to access an array element that do not exist.
      Java is zero based, this means that an array of size 3 have elements number 0, 1 and 2.

      at Abc.answer(Abc.java:14)



      这里给出错误的位置。

      不幸的是,此代码不完整,第14行不使用数组。



      通过使用调试器,您将能够在错误点检查变量并查看确切的操作数问题。



      有一个工具可以让你看到你的代码在做什么,它的名字是调试器。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

      当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

      使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



      调试器 - 维基百科,免费的百科全书 [ ^ ]

      http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

      https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]

      调试器在这里向您展示你的代码正在做,你的任务是与它应该做的事情进行比较。

      调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个bug。


      Here you are given the position of error.
      Unfortunately, this code is not complete and line 14 do not use array.

      By using the debugger, you will be able to inspect variables at point of error and see exactly what os the problem.

      There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
      When you don't understand what your code is doing or why it does what it does, the answer is debugger.
      Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

      Debugger - Wikipedia, the free encyclopedia[^]
      http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
      https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
      The debugger is here to show you what your code is doing and your task is to compare with what it should do.
      There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


      我怀疑问题如下:

      I suspect the problem is the following:
      ArrayList<integer> arr1 = new ArrayList<integer>(Arrays.asList(a[0][l]));
      ArrayList<integer> arr2 = new ArrayList<integer>(Arrays.asList(a[1][l]));
      //
      // a[0][l] is not an array, but a reference to a single element
      //
      //
      // Try:
      ArrayList<integer> arr1 = new ArrayList<integer>(Arrays.asList(a[0]));
      ArrayList<integer> arr2 = new ArrayList<integer>(Arrays.asList(a[1]));



      另外,为什么要创建两个数组 un un1 ,既然你从不使用它们?


      Also, why do you create the two arrays un and un1, since you never use them?


      这篇关于Java数组出界问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆