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);
}
tempis holding aarraylist type elementand sadly it isemptytempis never populated, which means any call totemp.get()will throw an error.