Overview

This tutorial will explain arrays and vectors. You will learn the difference between the two, and when to use each of them. Java implementations of each will be discussed and a few important API functions to use with each.

Arrays

An array is a data structure that stores a sequence of consecutively numbered objects. Each object can be accessed by using its number, or index. In many languages, including Java and C++, when you declare an array, you set its size. The array can not hold any more elements than its initial size. For example:

int stuff[] = new int[5];

This will create an array for holding integers that will hold up to five integers. If you want to add a 6th integer, you have to declare a larger array and copy all of the elements from the old array in to the new one and then add the 6th integer to the new array. Yuck! (This is where vectors come in handy). But first, let's talk about a few handy features in Java for handling arrays. A very important attribute of Java arrays is length. For any array, you can use this to find out how many elements it holds. This is very useful in for loops. Let's say we had an array numbers and we wanted to sum all the numbers in our array up, but we didn't know how big the array was. That's easy:

int sum = 0; for(int i = 0; i < numbers.length; i++) { sum += numbers[i]; }

Another way of doing this is using Java 1.5's new for-each loop. However, this type of loop does not allow you to directly change data within the array. It also doesn't require you to know the size of the array, just it's data type.

for(int i: numbers) sum += i;

Notice in this loop, i is no longer an index, but the actual element of the array. This type of loop will cycle through every element in your array and then stop on its own. The programmer has very little control over this loop, but it is still very powerful.

A useful function when declaring an array is fill(). Let's say you want to make an int array where all elements start as 2 for some reason. This can be done with a for loop, but it can be done much easier with the fill() function of the Arrays class.

int twos[] = new int[100]; Arrays.fill(twos, 2);

Or, let's say that you have a list of words that you need to put in alphabetical order (you can sort numbers also). Just use the Arrays function sort().

String dict[] = {"apple", "abe", "ant", "antonym"}; Arrays.sort(dict);

Be careful, sorting sorts by the ascii values of strings, so all capital letters come before lowercase letters (Zebra comes before aunt). The last function that may come in handy is toString(). This will put all of your elements in a comma seperated list that is surrounded by square brackets.

String battleship[] = {"A01", "B02", "B01", "C05", "D08"}; System.out.println(Arrays.toString(battleship));

Arrays are very easy to work with and should be used instead of vectors in any case where you are working with data of a fixed size. Like if you will always be handling five elements.

Vectors

A vector, as hinted above, is a rescaleable array. This is a data structure where all of the functions to resize when elements are added are already built in so that the programmer does not have to worry about them. Luckily, Java has Vector as a datatype for us to use. Another thing that vectors can do that arrays can not is hold multiple datatypes within one structure. A vector's first element can be a character and it's second can be an int. There is no type definition needed. To declare a Vector, we do it the same way as normal objects.

Vector example = new Vector();

If we declare the Vector in this way, all of its elements will be Objects that can be of whatever type we want. You will have to cast the objects when you retrieve them from the vector to use them in different ways (no fun). If you know before hand that you want your vector to hold only strings or ints or another datatype, you can declare a templated vector like this.

Vector<String> example = new Vector<String>(); Vector<Integer> example = new Vector<Integer>();

Now, if we want to add the word "bob" to the end of our vector, we simply use the add() function.

example.add("bob");

The same function also allows us to choose where in the vector we want to add something by specifying an index (like arrays). This will push the rest of the vector forward one index.

example.add(0,"before");

If you already have something at a location, and you want to change it to another value, use a set() method.

example.set(0,"new before");

Now, you know how to put elements in to the vector, you may be wondering how to access these elements. You can't access elements using [0],[1], etc. Instead you use the get() function with the same indices.

example.get(0);

Vectors have a toString() method that works the same way as the Arrays.toString() method, and instead of .length, they have a function size() that you must use.

System.out.println(example.toString()); example.size();

There are two handy functions for removing elements within your vector, remove() will remove an element at a specified index, and clear() will (as the name suggests) remove everything from your vector.

example.remove(1); example.clear();

Now, suppose you found a problem easier to solve using a vector, but in the end you are supposed to return an array. Don't fret, there is a simple method copyInto() which will allow you to do this in a snap.

String sentence[] = new String[example.size()]; example.copyInto(sentence);

Challenge

We'll combine the last few lessons together into today's challenge, and it will be much tougher than the previous challenges. Good Luck!

Program
This will be a game similar to BINGO, but easier. You should write a function that takes as input an array of strings card This array will contain exactly five strings. Each string will contain five numbers delimited by colons. Your program will also take as input, a string calls which will be a list of numbers delimited by commas. For each number in the calls, you will check your card for matches. Your program should return the string representation of the first row that contains a match for each of its five numbers (In the order given in calls not the order on the card), delimited with dashes! Assume calls are made consecutively in the order given. If there are no rows that match, then return the string "No winning rows." If multiple rows win at the same time, then return the row who got an individual number match first. (No rows will tie on this second criteria)

Sample Inputs:
We'll use the same card for all examples, and the calls will change.

card = {"1:2:3:4:5" , "2:5:7:8:9" , "11:12:13:14:5" , "4:9:51:12:44" , "1:2:53:21:23"};
calls = "1,2,11,12,9,5,3,4";
calls = "1,2,9,23,21,7";
calls = "1,2,11,13,12,14,4,3,5";
calls = "11,2,1,13,12,14,4,3,5";
calls = "5,9,4,12,44,51,2,7,8";

Sample Outputs:
"1-2-5-3-4"
"No winning rows."
"1-2-4-3-5"
"11-13-12-14-5"
"9-4-12-44-51"

plants