Tag Archives: Java

Arrays

Based on what we know, how do we represent a collection of things in a computer?

We must instantiate a variable for each thing we want to keep track of. For instance, this is a way that I could represent students in a classroom:

String student1 = "David";
String student2 = "Ethan";
String student3 = "T.V.";
String student4 = "James";
...

Another example is a collection of test scores from an exam:

int score1 = 70;
int score2 = 86;
int score3 = 94;
int score4 = 89;
…
int score25 = 32;

Now what if I wanted to average all of the test scores, what do I have to do?

double average = (score1 + score2 +score3 + score4 + … + score25) / 25; // this would be a super long line if completely filled out

Wouldn’t it be nice if we could manage collections of similar items in a more succinct way? Wouldn’t it be nice to reference the collection as a whole and grab more granular items when we want?

We can manage collections with arrays!

What is an array? It’s a data structure that has a fixed length and can hold 0 or more items of a specific type.

In Java arrays have the following syntax:

 
type[] variableName = new type[]; // the length of the array denotes the maximum number of elements you can store in the array.

Here’s an integer array that can hold 10 elements:

int[] myIntegerElements = new int[10]; // how many elements are inside of the array?

You can access elements inside of the array by indexing the array. Check out the two examples below. You’ll notice that you need to use brackets with the position number of the element you want to retrieve after the variable name. Keep in mind that arrays are zero based indexed so the first element is referenced by asking for the item indexed by zero.

int firstElem = myIntegerElements[0]; // what is the value stored in firstElem after this line executes?

What if I want the last element?

int lastElem = myIntegerElements[9]; // what is the value stored in lastElem after this line executes?

Also, arrays have instance variables that are pretty useful. For example, an array can tell you its length.

int arrayLength = someRandomArray.length; // assume someRandomArray is already declared and defined.

In what ways is an array’s length useful?

Say I wrote a function that accepts an integer array as an argument and it averages the numbers. What would that function look like?

public double AverageNumsInArray(int[] numArray){

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

What if I wanted a function that accepted an integer array and an integer called numToLookFor as arguments and it told me whether or not the integer numToLookFor existed inside of the array. What would that function look like?

public bool DoesIntegerExistInArray(int[] numArray, int numToLookFor){
    for(int i = 0; i < numArray.length; i++){
        if(numArray[i] == numToLookFor){
            return true;
        }
    }
    return false;
}

Where are those installers?

Last week I spent a couple of days looking for an open source cross-platform installer that can dynamically download data (dependencies) from the web. I couldn’t find anything that suited my needs. 🙁 Subsequently, I’m creating an installer from scratch for KIND, an automatic verification tool for Lustre programs.

It’s a fun project, but I’m stuck using Java. I’m not a huge fan of Java when a GUI is required. I’m creating a GUI based installer. Thankfully, the NetBeans IDE supports drag and drop GUI development but looking at the auto-generated code-behind, which is mixed with developer code, looks horrible – it’s esoteric and verbose!