Life long sharing . . .

Java – Arrays

Posted on: November 25, 2015

Lesson8-note1

Lesson8-note2

Lesson8-note3

Let’s try this exercise. Try amend the codes to get 1) the total of values and 2) the largest value in the array.

Lesson8-code1

Now, try to add this line to the end of the code and try running the program. What will you get?

Lesson8-code2

Yes, ArrayIndexOutOfBounds errors occur whenever you exceed the maximum index in an array. Remember, array indices are 0-based.

Two dimensional Arrays:

Two dimensional arrays are declared, allocated and initialized much like one dimensional arrays. However we have to specify two dimensions rather than one, and we typically use two nested for loops to fill the array.

Try below codes.

Lesson8-code3

This is the picture representing my2DimList:

Lesson8-code4

Oh ya, I almost forget this.

Lesson8-note4.png

For example:

Lesson8-code5

Try to compile and run this program as below. What is the output for each of the command below?

java MainArray

java MainArray Jan

java MainArray Nov 2005

java MainArray 2015

Reference:

http://www.tutorialspoint.com/java/java_arrays.htm

http://www.freejavaguide.com/arrays.htm

Advertisements

1 Response to "Java – Arrays"

2.6
1.9
4.2
3.5
Total is 12.2
Max is 4.2
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 4 at TestArray.main(TestArray.java:24)

ERROR: Missing parameter. You need to run this program with two arguments:
1. Birth month E.g. Dec
2. Birth year E.g. 1970

Birth month: Jan
Please enter your birth year. E.g. 19701

Birth month: Nov
Birth year: 2005

Birth month: 2015
Please enter your birth year. E.g. 19701

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Archives

November 2015
M T W T F S S
« Oct   Dec »
 1
2345678
9101112131415
16171819202122
23242526272829
30  

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 180 other followers

%d bloggers like this: