Problem with Arraylist

Asked

Viewed 631 times

3

I’m learning about Array and I’m racking my brain with this code, can someone explain to me why it’s not working?

This error appears

The type List is not Generic; it cannot be parameterized with Arguments

in this line List<Integer> lista = new ArrayList<Integer>();

package DeclaracaoArray;

import java.awt.List;
import java.util.ArrayList;
import java.util.Collections;

public class Declaracao_Array {


    public int sorteia(){
            List<Integer> lista = new ArrayList<Integer>() ;

            lista.add ( "Alice" ) ;
            lista.add ( "Bruno" ) ;
            lista.add ( "Carlos" ) ;
            lista.add ( "Daniel" ) ;

   Collections.shuffle ( lista ) ;

       // pega qualquer indice. pegamos o primeiro para conveniencia.

       return (( Integer ) lista.get ( 0 )).intValue () ;
    }
}
  • 1

    Don’t edit the question that way, you’ve completely changed what you were asked before and now it seems that the answers you post don’t make sense. If that problem has been solved, mark the correct answer and then create a new question to address that new bug you are having.

  • @Renan the problem as a whole (with arraylist) continues Renan, only changed the error message, so I did not create another question.

  • 1

    The first problem was that you were importing the object List wrong package. Now it’s because your class doesn’t have a method main.

  • I get it @Renan, I’ll close this question, thank you!

2 answers

6


You imported the wrong package:

import java.awt.List;

When it was meant to be:

import java.util.List;

As can be seen in the documentation List that you used does not make use of the generics, while the List that you wanted to use yes.

  • I changed the import and also changed to List<String> list = new Arraylist<String>(); and now the error is in Return The method intValue() is Undefined for the type String

  • 1

    If you added Strings to your list your method should return String and no longer int, thus: public String sorteia(){ and Return looks like this: return lista.get ( 0 );

5

You are defining that the list will contain variables of the type Integer while you are filling in with variables of the type String who are the names.

If you will not add a value that type is different from String declare the list variable this way:

List lista = new ArrayList();

Or

List<String> lista = new ArrayList<String>();
  • continues to appear the same error: The type List is not Generic; it cannot be parameterized with Arguments <String>

  • Change the import as from awt to util as mentioned by Math, what I said now will help you soon when you have arranged the import (;

  • I changed the import and also changed to List<String> list = new Arraylist<String>(); and now the error is in Return The method intValue() is Undefined for the type String

  • Now will not return another Integer, but rather a String, this should already solve: return lista.get(0). And change the public int sorteia() for public String sorteia()

  • I’m sorry to bore you so much with this, but as I’m learning I’m cracking my brain rs I made the changes and the new error is at the time of execution now: The main method was not found in the Declaracaoarray class.Declaraca_array; set the main method to: public Static void main(String[] args) I will change the question with the new code.

  • 1

    @Pauloroberto you need to declare the main method the way the error is showing: public static void main(String[] args), however realize that your doubt has been changing over time and the answers become outdated, and if someone gets to the topic now will find that we respondents are crazy, because our answers no longer answer the question :)

  • @Math I got it, it was wrong.

Show 2 more comments

Browser other questions tagged

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