Click here to Skip to main content
15,909,324 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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..

Java
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);

        }

    }
}


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..
Posted
Updated 13-Jan-18 6:46am
v3

C#
a[0][l]

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.

You could also try and use the debugger which would allow you to spot and understand very quickly what is going on internally.
Kindly.
 
Share this answer
 
v2
Comments
Akshit Gupta 13-Jan-18 12:24pm    
@phil.o Sir, Are you referring to " ArrayList<integer> arr1 = new ArrayList<integer>(Arrays.asList(a[0][l])); " ?
phil.o 13-Jan-18 12:29pm    
For example, yes. Everywhere you are trying to access the element at l index, you will get an exception, because the greatest index will ever be l - 1.
Akshit Gupta 13-Jan-18 12:35pm    
Sir but in my limited knowledge, aren't we supposed to declare an arry like a[size][size] ? Like if we want 2*8 elements in array, we declare it as a[2][8].. Hence ArrayList<integer> arr1 = new ArrayList<integer>(Arrays.asList(a[0][l])); ..
phil.o 13-Jan-18 12:37pm    
Yes you declare it as a[size][size].
Each dimension will then contain size elements, but these size elements will be indexed from zero to size - 1.
Why is array indexing in Java start with 0?
Akshit Gupta 13-Jan-18 12:40pm    
Sir can you please point out the statement in which i am accessing it out of bound.. Will be great help..
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3

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)

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.
 
Share this answer
 
Comments
Akshit Gupta 13-Jan-18 12:27pm    
Sir, Sorry but updated error is : at Abc.answer(Abc.java:14) at Abc.main(Abc.java:58)..
Patrice T 13-Jan-18 12:39pm    
Update your question to show where is line 14 in your source code.
Akshit Gupta 13-Jan-18 12:43pm    
Done Sir
I suspect the problem is the following:
Java
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]));

Also, why do you create the two arrays un and un1, since you never use them?
 
Share this answer
 
Comments
Akshit Gupta 13-Jan-18 12:47pm    
Thanks Sir, it worked.. I forgot to remove the declaration statement for un and un1..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900