noMAD
noMAD

Reputation: 7844

Not able to understand array of classes

Here is a brief code snippet:

someMethod ClassA getClassA() {
  List<ClassA.ClassB> classAType = Lists.newArrayList();
  //Now classAType is loaded with stuff here
  return ClassA.load(classAType.toArray(new ClassA.ClassB[classAType.size()]));
}

public abstract class ClassA {
  //Constructor
  public static abstract class ClassB {
    //some method
  }
}

The part which I don't understand is the return statement.I wanted to know what does ClassA.ClassB[classAType.size()] imply

Upvotes: 0

Views: 107

Answers (3)

wassup
wassup

Reputation: 2503

The getClassA() method first creates a list with ClassA.ClassB objects returned by Lists.newArrayList(). Then, it returns ClassA by calling a static method ClassA.load() passing one parameter: an empty array of type ClassA.ClassB with the same size as the classAType.

This piece of code:

classAType.toArray(new ClassA.ClassB[classAType.size()])

may look a bit complicated but it just creates an array containing all elements from the classAType list.

Upvotes: 2

evanmcdonnal
evanmcdonnal

Reputation: 48096

It's setting the size of the array. However it's going to be 0 because you never put anything in the arraylist. The code doesn't make a whole lot of sense...

Upvotes: 0

Colin D
Colin D

Reputation: 5661

new ClassA.ClassB[classAType.size()]

returns an array of ClassA.ClassB objects of size classAType.size()

since ClassAType is a List created with Lists.newArrayList() (Lists.newArrayList() is not defined here so the next part is an assumption), classAType.size() is 0.

Upvotes: 0

Related Questions