The error:
java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.rangeCheck(ArrayList.java:657)
at java.util.ArrayList.get(ArrayList.java:433)
at com.codegym.task.task07.task0728.Solution.sort(Solution.java:41)
at com.codegym.task.task07.task0728.Solution.main(Solution.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
Am having trouble figuring out where the index Out of Bounds error is occurring. Any pointers would be helpful.
package com.codegym.task.task07.task0728;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
/*
In decreasing order
*/
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int[] array = new int[20];
for (int i = 0; i < 20; i++) {
array[i] = Integer.parseInt(reader.readLine());
}
sort(array);
for (int x : array) {
System.out.println(x);
}
}
public static int[] sort(int[] array) {
ArrayList<Integer> lst = new ArrayList<Integer>();
int[] templst = new int[20];
for (int i = 0; i < 20; i++)
{
templst[i] = array[i];
}
int[] newarr = new int[20];
lst.add(0);
lst.set(0, templst[0]);
for (int i = 1; i < 20; i++)
{
for (int j = 0; j < 20 ;j++)
{
if (templst[i] <= lst.get(j))
{
lst.add(j, templst[i]);
break;
}
}
if (templst[i] > lst.get(lst.size() - 1))
{lst.add(templst[i]);}
}
for (int i = 0; i < 20; i++)
{newarr[i] = lst.get(19 - i);}
return newarr; //write your code here
}
}