2

i am trying to solve this question using arraylist in java but there is some error. can you tell me what is it that m doing wrong ? I'm using eclipse indigo and its showing the following error:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
  at java.util.ArrayList.rangeCheck(Unknown Source)
  at java.util.ArrayList.get(Unknown Source)
  at hackerrank.maxsub(hackerrank.java:32)
  at hackerrank.main(hackerrank.java:16)
private static ArrayList<Integer> maxsub(int[] input) {
    ArrayList<ArrayList<Integer>> temp=new ArrayList<ArrayList<Integer>>();
    int j=0;
    Integer[] sumarray=new Integer[temp.size()];
    for(int i=0;i<input.length;i++)
    {

        if(input[i]>=0)
        {
            temp.get(j).add(input[i]);
            sumarray[j]=sumarray[j]+input[i];
        }
        else
        {
            j++;
        }

    }
    int maxsum=Integer.MIN_VALUE;
    int indexofmaxsum=-1;
    for(int a=0;a<sumarray.length;a++)
    {
        if(sumarray[a]>maxsum)
        {
            maxsum=sumarray[a];
            a=indexofmaxsum;
        }
    }

    return temp.get(indexofmaxsum);
}
4
  • what you actually trying to do? Commented Jun 14, 2016 at 3:43
  • here temp is holding a arraylist type element and sadly it is empty Commented Jun 14, 2016 at 3:45
  • It looks like temp is never populated, which means any call to temp.get() will throw an error. Commented Jun 14, 2016 at 3:45
  • please read this (javatpoint.com/ArrayList-in-collection-framework) for some basics Commented Jun 14, 2016 at 3:47

3 Answers 3

2

Your problem lies in below statement:

temp.get(j).add(input[i]);

You have created an Arraylist of ArrayList(containing Integers),

ArrayList<ArrayList<Integer>> temp=new ArrayList<ArrayList<Integer>>();

and you haven't added any element(ArrayList of Integers in this case) and trying to get an element using temp.get(j).add(input[i]);

You'll also experience problem here

  Integer[] sumarray=new Integer[temp.size()];

as you have instantiated an array of size 0(as temp is empty). And

  sumarray[j]=sumarray[j]+input[i]; 

this will also cause same exception

Sign up to request clarification or add additional context in comments.

2 Comments

then how do i add the value of input[i] to my arraylist of arraylist ?
Now you know, where the problem is. Try to figure out a way. But before that you need to get basics right. Follow a beginner's book and attempt standard algorithms first.
0

You should be aware temp.size is zero before assign to sumarray

Integer[] sumarray=new Integer[temp.size()];

Comments

0

Copy part of your code

ArrayList<ArrayList<Integer>> temp=new ArrayList<ArrayList<Integer>>();
    int j=0;
    Integer[] sumarray=new Integer[temp.size()];
    for(int i=0;i<input.length;i++)
    {

        if(input[i]>=0)
        {
            temp.get(j).add(input[i]);
            sumarray[j]=sumarray[j]+input[i];
        }
        else
        {
            j++;
        }

    }
  1. temp seems not populated before used so calling get(0) must throws null exception.
  2. Inner ArrayList instance did not initialized. Call add() function will also produce a null pointer exception alert.

Hope this helps you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.