Seriously screwed for Java assessment

Nope, don't have Java on this box. But your test is flawed regardless. You are still calling upon two objects .. the interface, and the object. "object" in my previous reply not being the programmatic definition of instantiated object, but "thing". Even on the vtable this makes a difference. When it references the interface on the object, verifying the interface is satisfied.

Your test is flawed, regardless. Compile two applications, one with List, one with ArrayList. Then compare. Not one with both.

and irrespective of this digression, why use "List" and not "ArrayList" when you are expecting an ArrayList, not a polymorphic "List," object?


WHa happens in future when you want switch out implementations of a List, having a handle to a list means you can use constructs like linked lists.

And whats wrong with using typesafety? we know the solutions needs a collection of Integers? so we should let the compiler enforce this no?
 
Here is a more efficient printMarks method, it only makes a single IO call and doesn't require a conditional statement to check for the last element. There also is no need for an Iterator.

Another way could be to use an enhanced for loop (for each loop) to do the same but you wouldn't be able to check for the last element efficiently (you could get the last element and then check if the current one is the last element but that is way less efficient). Another approach is to use the enhanced for loop and insert a comma after each element then create a substring of the final string to remove the comma but that is a bit of a kludge and probably no more efficient.


Code:
public void printMarks () {
	String out = "";
	for (int i = 0; i < marksArray.size()-1; i++) {
		out += marksArray.get(i)+", ";
	}
	out += marksArray.get(marksArray.size()-1);
	System.out.println(out);
}

nice i never thought of doing this, but there is a typo?
out += marksArray.get(marksArray.size()-1); no need for -1 i think
 
Here is a more efficient printMarks method, it only makes a single IO call and doesn't require a conditional statement to check for the last element. There also is no need for an Iterator.
One last change is to remove += for concatenating strings. It will kill performance as the string grows in size.

Strings being immutable cannot be modified and joining two will create a brand new String to hold the result.
Code:
String a = "this ";
a += "that";

The second line gets executed as
a = new StringBuilder(String.valueOf(a)).append("that").toString();

Have look here:
http://articles.techrepublic.com.com/5100-22-5031999.html

I would change PrintMarks() to
Code:
public void printMarks () {
	StringBuilder out = new StringBuilder(); //or StringBuffer pre 1.5
	for (int i = 0; i < marksArray.size()-1; i++) {
		out.append(marksArray.get(i));
		out.append(", ");
	}
	out.append(marksArray.get(marksArray.size()-1));
	System.out.println(out);
}
 
Just another variation on Rob's example, possibly more efficient with respect to string concetenation above a certain *magic* number of elements in the array:

Code:
 public void printMarks() 
 {
     Iterator it = marksArray.iterator();
     StringBuilder outputBuilder = new StringBuilder();
 
     while (it.hasNext()) 
     {
         if (outputBuilder.length() > 0)
         {
             outputBuilder.Append(",");
         }
         outputBuilder.Append(it.next());
     }
 
     System.out.print(outputBuilder.toString());
}

Apologies if there's a bit of C# syntax in the mix there.

EDIT: FourStar just beat me to it :)
 
Back
Top Bottom