0

I have a school assignment on immutable generic Binary Search Trees in java. I have implemented all of the basic BST functions but we are also asked to create a saveInOrder function that takes a generic array of type Entry that should be modified in the function to contain all of the Entries in order of the value of their key.

We are given an interface to implement, and in that interface there is a method signature

public void saveInOrder(Entry<Key,Value> a[]);

I'm confused as to why the [] brackets are after the 'a'? Shouldn't it be

Entry<Key,Value>[] a?

Also, will the array passed to this function have a size defined already? I googled generic arrays and have realised they're a tricky topic. Any explanations would be great thanks! Sorry if it's a little ambiguous

toastedDeli
  • 553
  • 5
  • 24
  • P.S he gave us this to help: Entry e = new Entry<>(key,value); @SuppressWarnings("unchecked") Entry[] a = (Entry[]) Array.newInstance(e.getClass(), size()); – toastedDeli Mar 01 '16 at 01:41
  • " will the array passed to this function have a size defined already?" - Yes, every array (except when a `null` reference was passed to your function) in Java has a defined length that cannot be changed after creation. – Erwin Bolwidt Mar 01 '16 at 01:42

1 Answers1

0

In Java, the brackets can be before or after the variable name (and both are semantically equivalent). It could also be written as

public void saveInOrder(Entry<Key,Value>[] a);

Also, will the array passed to this function have a size defined already?

Yes. JLS-10.3. Array Creation says (in part)

The array's length is available as a final instance variable length.

Elliott Frisch
  • 183,598
  • 16
  • 131
  • 226