0

How to assign a value to a list in java? If Ii assign a following value it shows cannot convert from int[] to List<Integer>

public class GetAddonProductsProcessorTest {

  @Test
  public void AddonProductstest() throws Exception{
    
    //BasicDBList newobj = new BasicDBList();
      
    List<Integer> newobj = {1,2,3};
  
  }

GameDroids
  • 5,584
  • 6
  • 40
  • 59
  • Reading through the [Javadoc for List](https://docs.oracle.com/javase/8/docs/api/java/util/List.html) will help you. List is an interface, you need to instantiate a new object of a type that implements it, such as `ArrayList` and then use the corresponding methods to add things to the list. – JustAnotherDeveloper Sep 04 '20 at 12:41
  • Try `List newobj = List.of(1, 2, 3);`. –  Sep 04 '20 at 13:42

1 Answers1

1

You can create list using Arrays.asList

ArrayList<Integer> newObj = new ArrayList(Arrays.asList(new Integer[]{1,2,3}));
Pshemo
  • 122,468
  • 25
  • 185
  • 269
Peter
  • 405
  • 3
  • 6
  • 14
  • There is no need for `new Integer[]` as varargs will let compiler generate it for us, so we can write easier code like `Arrays.asList(1,2,3)`. Also prefer to program on interfaces (more info: [What does it mean to “program to an interface”?](https://stackoverflow.com/q/383947)) so instead of `ArrayList newObj =` use `List newObj =` (like OP already does). – Pshemo Sep 04 '20 at 12:57