Arraylab10a Word
Arraylab10a Word
Array usage
Arrays may be declared in two ways, by putting brackets either after the Type or after
the array Name.
Arrays can be instantiated by either creating a new empty array or by passing it an array
literal.
So, if I wanted to build a String array of size 10, I'd simply write this:
When creating a new empty array, it behaves the same way declaring a new variable
would. Every variable in the array is given its default value. (0 for numbers, false for boolean,
‘\u0000’ for char, and null for any Reference Variables) Once the array is initialized with its
default values, these values can be used to store data.
The first index in any array is 0, not 1. This means an array of length n
will have indexes from 0 to n-1.
Exceptions occur when there is something that goes wrong during either compile time or
execution time. Using the previous example, we can look at the exception that is generated
when we run it, and determine what caused it. When inserting the code from the previous
example, it would look something like this:
When run, the console will display an exception that should look almost exactly like the
following exception:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at TestClass.otherMethod(TestClass.java:9)
at TestClass.main(TestClass.java:4)
C:\Users\Diego\AppData\Local\NetBeans\Cache\8.2\executor-
snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)
At first glance, the exception may look confusing, but it's actually simpler to read than it
seems. To read the exception, all we have to do is break it down into pieces:
java.lang.ArrayIndexOutOfBoundsException: 3
● This is the exception that was thrown. Note that there is a 3 after the
exception name. This is the exception trying to tell us that 3 was the index
that went out of bounds.
at TestClass.otherMethod(TestClass.java:9)
at TestClass.main(TestClass.java:4)
● This is known as the "Stack Trace". What this shows is exactly where in
your code the error was thrown. If there are multiple multiple methods
involved like in this example, it will show where exactly in each method
the error was thrown. The first part of each entry in the stack trace shows
which method and class the execution was in when the exception
occurred, while the second part of each entry describes what line of each
method the exception occurred at. In this example, main called
otherMethod at line 4, which then crashed at line 9.
By reading the pieces of an exception, it's possible to find exactly what line caused the
problem and why. In this lab, you will be using your knowledge of exceptions to understand how
arrays work and correcting code.
Task Overview
Task 1. What kind of errors are is caused by the following code, if any?
How would we fix them? Note: Some errors may be caused before execution
(before you run the program). In this case, list the error written in NetBeans when
you hover over the problem.
/*Task 1a.
Exception: ArrayIndexOutOfBoundsException
What caused the error: The string array is declared to go from 0-9 we
are going above its limit
Task 1b.
Exception: NegativeArraySizeException
Task 1c.
Exception: ArrayStoreException
What caused the error: The code looks for a String Array but the []
are missing
Task 1d.
System.out.println(arr[2].length());
Exception: NullPointerException
What caused the error: String started at Null and it was called
How to Fix It: Declare a value in arr [2]
Task 1e.
Exception: ArrayStoreException
What caused the error: Object was declared but a string array was
created
Task 1f.
*/
● Create a new Java project, and begin by asking the user how many values they would
like to store into an array.
● Initialize a global String array "data" using the input as size.
● Create a method called "populateArray" which will contain a loop that asks the user to
input each value and store them within the global array "data".
● Use a loop to print the contents of the changed array.
● Create a method called "changeArray" which will ask the user for an index (location) in
the array.
● Print out the value stored in the global array at the index specified by the user.
● Ask the user if they would like to change that value. If so, get a value from the user and
modify "data" at the specified index to match the user's input.
● Use a loop to print the contents of the changed array.
ArrayIndexOutOfBoundsException
This is caused when you try to access an invalid index in the array.
ArrayStoreException
This is caused when you try to store a value in the array that doesn't match the array's type
NegativeArraySizeException
This error is thrown when the array is initialized using a negative size
NullPointerException
This error is not specific to arrays, but is still very common when using them. Whenever an
array is created, the values of any reference variables (Objects) within it are null by default.
They only have a value once you assign a value to them.