Error in method of merging elements with Mergesort into a ListInteger

0

This part of the algorithm has the function of interleaving the elements of a List<Integer> (list of normal integers) using the Mergesort sort algorithm. ERROR ON LINE 7

private static List intercalar(List<Integer> list, int l, int h, int r){
    int i = l, j = h, /*marcador do topo*/t = 0; 
    List<Integer> topo = Arrays.asList(); 

    while(i < h && j < r){ // O(n/2) = 
        if(list.get(i) < list.get(j)){ 
            topo.add(t, list.get(i)); **ERRO AQUI**
            i++; 
        } else { 
            topo.add(t, list.get(j));
            j++; 
        } 
        t++;
    } 

    //anexa o restante das cartas(as cartas q ficaram sozinhas)
    while(i < h){
        topo.add(t, list.get(i));
        t++;
        i++;
    }
    while(j < r){
        topo.add(t, list.get(j));
        t++;
        j++;
    } 

    for(i = l, t = 0 ; i < r ; i++, t++) {
        list.set(i, topo.get(t));
    } 
    return list;
} 

Displays the following error:

  

java.util.UnsupportedOperationException: null (in java.util.AbstractList)

    
asked by anonymous 18.08.2017 / 20:40

1 answer

0
Arrays.asList(); 

It works as follows if you have a vector of objects that you want to allocate in a list, such as:

int[] vet = new int[]{1,2,3,4,5};
List<int> list1 = Arrays.asList(vet);

In this case I have placed a size 5 vector in a list, in your case you are not allocating anything in the list, so it is giving error because your list is empty. Another thing I've been looking at is your code and I see that you're trying to manipulate the list as if it were a map :

 topo.add(t, list.get(i)); **ERRO AQUI**
    i++; 
 } else { 
    topo.add(t, list.get(j));

Change to this:

 topo.add(list.get(i)); **ERRO AQUI**
    i++; 
 } else { 
    topo.add(list.get(j));

Or just use:

List<int> topo = new ArrayList<int>();
    
18.08.2017 / 20:52