Arrays in Java9-16-2004Opening DiscussionWhat 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.Why not make main simpler?Keeping Track of ThingsOne 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 ObjectsIn 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 ReferencesArrays 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 SyntaxWhen 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 ArraysA 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 ArraysAs 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 ClassYou 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 CodeNow 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 EssayWrite 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