title: Index 0 out of bounds for length 0, Java runtime error
body: <p>Here is my Hacker Rank code. This code is for solving the problem lexicographical order. Here w is the input string. the output string must be greater than the input string just by swapping the characters. but the swapping must be done such that the new string must be the minimum greater next string of the previous string:</p>
  <pre><code>    boolean found=false;
      //char[] arr = new char[4];
      char bigger=arr[arr.length-1];
      int index=0;
      for(int i=arr.length-1;i&gt;0;i--){
          if (arr[i-1]&lt;arr[i]){
              char smaller=arr[i-1];
              index=i-1;
              found=true;
              break;
          }
      }
      char smallerchar=arr[index];
      char[] subarray1=Arrays.copyOfRange(arr, 0, index);
      char[] subarray2=Arrays.copyOfRange(arr, index+1, arr.length);
      List combinedlist = new ArrayList(Arrays.asList(subarray1));
      combinedlist.addAll(Arrays.asList(subarray2)); 
      List&lt;Integer&gt; ascii=new ArrayList&lt;Integer&gt;(); 
       char[] combinedarray = new char[combinedlist.size()];
      for (int i = 0; i &lt; combinedarray.length; i++) {
          combinedarray[i]=(combinedlist.get(i)).toString().charAt(0);            
      }
      for (int i=0;i&lt;combinedarray.length;i++){
         -------&gt; ascii.set(i,(int)combinedarray[i]);
       
      }
      Collections.sort(ascii);  
      int[] asciisorted = ascii.stream().mapToInt(i-&gt;i).toArray();
      for (int i=0;i&lt;combinedlist.size();i++){
          combinedlist.set(i,(char)asciisorted[i]);
      }
      Character[] sortedarray = (Character[])combinedlist.toArray(new Character[combinedlist.size()]);
      String sorted=Arrays.toString(sortedarray);
      String ssmallerchar=Character.toString(smallerchar);
      w=smallerchar+sorted;
      if(found){
          return w;
      }
      else{
          return &quot;no answer&quot;;
      }
  </code></pre>
  <p>The line with the arrow is showing runtime error index 0 out of bounds for length 0. Kindly help. Thanks in advance.</p>