Error in the interlayer method elements with Mergesort in a List<Integer>

Asked

Viewed 101 times

0

This part of the algorithm has the function of bridging the elements of a List<Integer> (list of normal integers) using the Mergesort sorting 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)

  • Amigo presented the following error : java.util.List is Abstract; cannot be instantiated.

  • I published an answer there tries to see if it solves your problem

  • I listed the errors that appeared...

1 answer

0


Arrays.asList(); 

It works as follows, if you have an object vector you want to allocate in a list as for example:

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

In this case I have put 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. Something else I’ve been watching your code and I’ve seen 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));

Switch to this:

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

Or just use it:

List<int> topo = new ArrayList<int>();
  • Friend, using the List<Integer> top = new Arrays.asList(); appeared the following error: cannot find Symbol - class asList and using List<Integer> top = new List<Integer>(); appeared: java.util.List is Abstract; cannot be instantiated.

  • Edited, try to take my example

  • I understand, so in this case I would have to be a list within that new list

  • it wouldn’t have to be a vector inside the list, so the method is called Arrays.asList hehe

  • friend...

  • what was the change that Voce made ?

  • I put the elements of my first list inside an array and then created the new list by passing the array the way you taught it. Gave null error on add() line again...

  • can edit your question and place and show what has been edited ?

  • //Integer[] vet = new Integer[list.size()]; Integer[] vet = {5,7,7,8,8,1,5,5,7,7,8,1,1,5,5,7,7,8,8,1}; //for(int z = 0; z < list.size(); z++)and#Xa; // vet[z] = list.get(z); //} List<Integer> top = Arrays.asList(vet);

  • I edited my answer, from a analyzed and check

  • gave exactly the same error...

  • Voce has to check tmb if the value j and h are not overflowing the list, like if the list has size 10 and j or h is 11 understood ? your code is a bit confused so I recommend you start debugging it from the first line also recommend you use List<int> top = new Arraylist<int>();

  • I got it buddy, thank you very much !

  • what was the change ?

  • List<Integer> topo = new ArrayList();&#xA; &#xA; while(i < h && j < r){&#xA; if(list.get(i) < list.get(j)){ &#xA; topo.add(t, list.get(i)); &#xA; i++; &#xA; } else { &#xA; topo.add(t, list.get(j));&#xA; j++; &#xA; } &#xA; t++; &#xA; }

Show 10 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.