user4593157
user4593157

Reputation: 95

what does java.util.Arrays$ArrayList mean?

I keep getting the following error message

File: C:\Users\jiangbuyun\Desktop\p5\p5pack\P5Tests.java  [line: 1084]
Failure: java.lang.AssertionError: expected: java.util.Arrays$ArrayList<[A:X, B:X, C:X]> but was: java.util.ArrayList<[A:X, B:X, C:X]>

I have a class called circuit. Here are some fields.

private List<Contact> inputs, outputs;
private List<Wire> innerWires;

and this is the method how I get inputs

public void parseContactsLine(String line){
    Scanner sc = new Scanner(line);
    int i=0; //token's position
    while (! sc.hasNext("->")){
      Wire w = new Wire (sc.next());
      innerWires.add(w);
      Contact c = new Contact(innerWires.get(i),innerWires.get(i),true);
      inputs.add(c);
      i++;
    }

My test case:

public void circuit_parseContactsLine1(){
    List<Contact> empty = Arrays.asList(new Contact[]{});
    assertEquals(empty, vanillaCircuit.getInputs());
    assertEquals(empty, vanillaCircuit.getOutputs());
    List<Contact> ins = Arrays.asList(new Contact[]{
      new Contact(new Wire("A"), new Wire("A"), true),
        new Contact(new Wire("B"), new Wire("B"), true),
        new Contact(new Wire("C"), new Wire("C"), true)}
    );
    List<Contact> outs = Arrays.asList(new Contact[]{
      new Contact(new Wire("D"), new Wire("D"), false)}
    );

    vanillaCircuit.parseContactsLine("A B C -> D");
    assertEquals(ins , vanillaCircuit.getInputs ());
    assertEquals(outs, vanillaCircuit.getOutputs());
  }

It seems this error arises from the comparison between ArrayList and List. The test case uses Array.asList() to get "inputs", and my method returns an arraylist as well. Can anyone explain to me what java.util.Arrays$ArrayList mean?

The error when I am trying input.add(Arrays.asList(c));

File: C:\Users\jiangbuyun\Desktop\p5\p5pack\Circuit.java  [line: 48]
Error: no suitable method found for add(java.util.List<Contact>)
    method java.util.Collection.add(Contact) is not applicable
      (argument mismatch; no instance(s) of type variable(s) T exist so that java.util.List<T> conforms to Contact)
    method java.util.List.add(Contact) is not applicable
      (argument mismatch; no instance(s) of type variable(s) T exist so that java.util.List<T> conforms to Contact)

Upvotes: 6

Views: 8482

Answers (1)

Eran
Eran

Reputation: 393936

java.util.Arrays$ArrayList is a nested class inside the Arrays class that implements the List interface. This implementation has a fixed length and is backed by an array.

The method Arrays.asList returns an instance of this class.

Upvotes: 7

Related Questions