DOC PREVIEW
TRINITY CSCI 1321 - Arrays in Java

This preview shows page 1-2-3-4 out of 12 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Arrays in Java2-3-2005Opening DiscussionWhat did we talk about last class?Do you have any questions about assignment #2? Problem code to show?The design of classes does include the methods that are in them. They need to be stubbed out and documented. Your design should have all the methods you expect to write, unimplemented. Each should be properly documented.String Parsing CodeLet's take a few minutes to look over/complete the code we started last time.Keeping Track of ThingsOne of the things that we love to be able to do in programs is to be able to keep track of the information we are using. That is to say that we want to be able to save off values and pull them back for later use.The simplest structure for doing this in Java (and many other languages) is an array. Basically an array gives us a way to refer to things with numeric indices.Arrays are ObjectsIn Java, arrays are objects. For every type, A, in Java, there is a type that represents and array of A that we write as A[].As with other objects in Java, what we really declare is a reference to that object and we have to allocate it before we can use it.The syntax otherwise is much like in C/C++.Because they are objects, arrays know their length. There is a public final integer member of the array classes called length that you can read it from.Arrays of ReferencesArrays of primitive types act the same way in Java as in C/C++. However, arrays of objects are really arrays of references to objects.This enables polymorphism, but also requires that each object in the array must be allocated individually before it can be used. Prior to your allocation, they are all set to null.Initialized Array SyntaxWhen you declare an array in Java you can use special syntax to initialize it. You only want to do this with short arrays.The syntax you use is to have a comma separated list of array elements in curly braces like this. This is most helpful for small arrays of primitives.int[] a={4,7,2,-9,2};Multidimensional ArraysA natural extension of arrays is to have arrays of several dimensions. In Java this is accomplished by using the potentially recursive definition of arrays.Remember I said that any type can be used with an array and the array of the type is a type itself. This means that the type in an array can be an array type.Basically you can put as many [] after a type as you want.Allocating Multidimensional ArraysAs with any array of objects, allocating the array doesn’t allocate the elements in the array, just space for references to them. You need to allocate the arrays in the array yourself.If you are allocating a “rectangular array” Java has an abbreviated syntax where you can specify the size of many dimensions in one new statement.Your Screen ClassYou are going to need to use a 2D array in your screen class to hold the blocks that make up the screen.The blocks can accessed by x,y coordinates placed in the normal way in computer graphics with 0,0 at the top left.Let’s also look at the two methods that help the screen editor interact with the blocks on your screen.Let’s CodeNow we want to write some code that demonstrates the use of arrays and/or 2D arrays in Java and shows how the fact that they are references means that they can be polymorphic.Minute EssayWrite code to declare two 2D arrays of type Shape. Make the first a square of 6x6. Make the second a triangle with the length of the “second” array equal to the first index plus one.The design for assignment #2 is due next


View Full Document

TRINITY CSCI 1321 - Arrays in Java

Documents in this Course
Recursion

Recursion

11 pages

Iterators

Iterators

10 pages

Actors

Actors

9 pages

Recursion

Recursion

15 pages

Recursion

Recursion

10 pages

Threads

Threads

7 pages

Trees

Trees

11 pages

Load more
Download Arrays in Java
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Arrays in Java and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Arrays in Java 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?