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)
This method sorts the specified array of objects into ascending order, according to the natural ordering of its elements. This method sorts the specified range of the specified array of objects into ascending order, according to the natural ordering of its elements.
This method inserts the specified element at the specified position in this list. This method inserts all of the elements in the specified collection into this list, starting at the specified position. This method removes all of the elements from this list. This method returns a shallow copy of this ArrayList instance.
The array is a fixed sized data structure thus, the array always needs to mention the size of the elements. On the other hand, ArrayList is not a fixed sized data structure, thus there is no need to mention the size of the ArrayList especially creating its object.
ArrayList is a part of collection framework and is present in java. util package.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With