Lab 10 -- Arrays and Vectors

1. Brief review of arrays and vectors

2. Example: selection sort of arrays.

3. Practice writing methods for arrays and vectors

To get started: Download the lab10_programs folder from nike, open the labArrayVector subfolder, and add your code to labArrayVector.java . Then run the project labArrayVector.mcp. You can test your code by running the project test.mcp in the test subfolder of the labArrayVector folder.

Part 3a: Printing out the contents of an array

 We want to be able to print out the contents of our arrays (so then when we test our array methods, we can print them out to make sure that our methods are working). Let's write a method called printArray(int [] a) that prints out the contents of the integer array a on one line. For example, say we have an array B = [ 2 4 6 8 ]. Then invoking printArray(B) should produce the following in the Java Console window:

Array B: [ 2 4 6 8 ]

 

System.out.print() vs. System.out.println().

print() and println() are two methods that allow you to print text to the stdout window.

print() prints your text and leaves the cursor at the end of the line, ready to print the next thing.

println(), on the other hand, prints your text and then waits at the start of the next line to print the next thing.

This is best demonstrated by example. Each snippet of code produces the window below it.


		System.out.print("Hi, there!");
		System.out.print("Good");
		System.out.println(" to see you!");

(Note: no spaces between there! and Good )

		System.out.println("Hi, there!");
		System.out.println("Good");
		System.out.println(" to see you!");


		System.out.println("Hi, there!");
		System.out.print("Good");
		System.out.println(" to see you!");

Use these examples to format the printing of elements of an array. In this method we print all elements on one line.

Part 3b: Reversing the contents of an array

Now we want to reverse the contents of our array. So, if array C= [0 1 2 3 4] then reverse(C) should produce [4 3 2 1 0]. Now, aren't you glad that we wrote printArray above? We can use that to check that our reverse actually works! Our test code looks something like this:


      int []  myArray = { 9, -1, 3, 6, -10}; // creates an array named myArray
      
      IntArray.printArray(myArray); // prints out the contents of myArray
      IntArray.reverseArray(myArray); //reverses the contents of myArray

						System.out.println("Reversed myArray"); // prints out a message to inform user 
																																																																					// that the array was reversed
      IntArray.printArray(myArray); // prints out the reversed contents of myArray
      
  

Part 3c: Printing out the contents of a vector

Same thing as Part 3a above, except now we're working with vectors.

Part 3d: Reversing the contents of a vector

Same thing as Part 3b above, except now we're working with vectors.

(some useful methods from the) Vector contract

public Vector() -- constructs a new empty vector

public Vector(int n) -- constructs a new vector containing n elements

public int size (Vector v) -- returns the number of items in the vector

public final void addElement(Object obj) -- adds the object obj to the back end of the vector

public final Object elementAt(int index) -- returns the Object at index in the vector

public void insertElementAt (Object obj, int index) -- inserts the object obj at index in the vector

public void setElementAt(Object obj, int index) -- sets the object at index to be obj