September 20, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Objects and Collections

  • July 10, 2006
  • By Matt Weisfeld
  • Send Email »
  • More Articles »

This series, The Object-Oriented Thought Process, is intended for someone just learning an object-oriented language and who wants to understand the basic concepts before jumping into the code, or someone who wants to understand the infrastructure behind an object-oriented language he or she is already using. These concepts are part of the foundation that any programmer will need to make the paradigm shift from procedural programming to object-oriented programming.

Click here to start at the beginning of the series.

In keeping with the code examples used in the previous articles, Java will be the language used to implement the concepts in code. One of the reasons that I like to use Java is because you can download the Java compiler for personal use at the Sun Microsystems Web site http://java.sun.com/. You can download the standard edition, J2SE 5.0, at http://java.sun.com/j2se/1.5.0/download.jsp to compile and execute these applications. I often reference the Java J2SE 5.0 API documentation and I recommend that you explore the Java API further. Code listings are provided for all examples in this article as well as figures and output (when appropriate). See the first article in this series for detailed descriptions for compiling and running all the code examples.

In the previous column, I covered the topic of primitives (basically the low-level system variables), objects, and wrappers. This discussion included all of the primitive types, as well as a brief description of how strings relate to these primitives. At the end of the article, I touched upon some of the JDK 1.5 Enhancements, specifically Autoboxing and Unboxing.

Interestingly, one of the best examples of object wrappers is provided by the JDK itself in the form of the primitive wrappers such as the classes Integer, Double, and objects. When objects are created from these classes, they literally wrap a single primitive inside that object. This creates a lot of overhead for the single primitive; however, it provides much-needed behavior to use and process the primitive. Wrapping the primitive also allows the value stored by the primitive to be used as on object. This is an issue that you will explore in more detail later.

These wrappers hold a single value. Obviously, there are times when you are interested in working with multiple values—this is where the term collections enters the discussion. In this article, you will start the exploration of how objects relate to collections. This is a direct progression from the discussion of primitives. You will start by covering arrays and will consider the advantages and disadvantages of the original concept of an array. Next, you will cover how collections such as ArrayLists provide additional functionality.

Arrays

Because this series focuses on object concepts, you will not explore the syntax of arrays in great detail; however, you will touch upon some of the interesting ways that arrays are influenced by the object-oriented paradigm and how they relate to objects.

The Sun documentation states that the Java programming language arrays are objects, are dynamically created, and may be assigned to variables of type Object. All methods of class Object may be invoked on an array.

Declaration & Instantiation

Start out with a simple piece of code (this code has a bug):

public class ArrayLists {
   public static void main(String[] args) {
      int items[];
      items[1] = 5;
      System.out.println("items[1]=" + items[5]);
   }
}

Listing 1

The syntax for creating an array is as follows:

int items[];

One of the most confusing issues of moving from C/C++ to Java involves syntax such as you have in the previous line. In this case, a C/C++ programmer would identify right away that the size of the array is missing.

A C/C++ programmer is used to creating an array in one, concise statement like this:

int items[5];

In C/C++, the memory for the array is calculated by the compiler and allocated without having the programmer explicitly creating it. Thus, the instantiation and the creation are done together.

However, in Java and other object-oriented implementations, this is not the case. The declaration and the instantiation are two distinct processes. This is why there is a bug in the following code.

int items[5];

This line contains only the declaration of the array. If you try to compile this, you receive the following error:

C:column24>"C:Program FilesJavajdk1.5.0_06binjavac" ArrayList.java
ArrayList.java:7: variable items might not have been initialized
          items[1] = 5;
          ^
1 error
C:column24>

Although this error might seem a bit cryptic, it is actually identifying the fact that the array pointer (the variable items) has yet to be instantiated. In other words, the pointer does not point at anything yet because the memory for the array has not been allocated.

To allocate the memory for the array, you must add a second part to this process.

items = new int[5];

This line identifies that the array is to consist of five integers. It makes sense that the compiler needs to know what the array will consist of and how many of these items there are. In this example, you initialize items[1] to the value 5 and then simply print it out. The code in Listing 2 does compile cleanly.

public class ArrayList {
   public static void main(String[] args) {
      int items[];
      items = new int[5];
      items[1] = 5;
      System.out.println("items[1]=" + items[1]);
   }
}

Listing 2

When this code is executed, the following output is generated (the actual program output is in bold).

C:column24>"C:Program FilesJavajdk1.5.0_06binjava" Te
items[1]=5
C:column24>

Although you have separated the declaration and the instantiation in this example by using two separate lines, it is possible, of course, to combine the two processes in the same line. In fact, this is the syntax normally seen.

int items[] = new int[5];

Initializing Arrays

Some of the more interesting array examples require that you first initialize the arrays to fully demonstrate the code. Consider the following code:

int[] items = {0,1,2,3,4};

In this line of code, you explicitly initialize the array as you declare and instantiate it. Note that there is no requirement for the new keyword. By using this syntax, you instruct the compiler to allocate the memory for the array and initialize each of the array items. Also note that there is no need to specify the size of the array; the compiler can figure that out by the number of entries within the declaration brackets.

Now, introduce a loop into this simple example and you will be ready to explore some interesting array/object connections.

public class ArrayLists {
   public static void main(String[] args) {
      int[] items = {0,1,2,3,4};
      for (int i = 0; i<5; i++) {
         System.out.println("items[" + i + "]=" + items[i]);
      }
   }
}

Listing 3

When this code is executed, the following output is generated (the actual program output is in bold).

C:column24>"C:Program FilesJavajdk1.5.0_06binjava" ArrayLists
items[0]=0
items[1]=1
items[2]=2
items[3]=3
items[4]=4
C:column24>

This code is basically a benchmark. By creating this simple example, we can use it as a baseline for everything that we do going forward. This process allows us to provide the infrastructure for all of our future examples. Perhaps more importantly, it provides a testing framework that will allow us to concentrate on the development process of our examples.

You also can create a multi-dimensional array. The syntax for a multi-dimensional array can be quite confusing. For example, create a 2-dimensional array.

int[][] items = { {0,1,2,3,4},
                  {5,6,7,8,9},
                  {10,11,12,13,14},
                  {15,16,17,18,19},
                  {20,21,22,23,24}};

Again, note that there is no need to specify the size of the array; the compiler can figure this out by simple counting. The one thing that I like to do in the case of multi-dimensional arrays is to format my code so that it is obvious where the dimensional boundaries are. In this case, the 2-dimensional array boundaries are obvious because they are grouped visually as 5 by 5. Grouping in this way can save a lot of syntax headaches.

public class ArrayLists {
   public static void main(String[] args) {
      int[][] items = { {0,1,2,3,4},
                        {5,6,7,8,9},
                        {10,11,12,13,14},
                        {15,16,17,18,19},
                        {20,21,22,23,24}};

      for (int i = 0; i<5; i++) {
         for (int j = 0; j<5; j++) {
            System.out.print(items[i][j]+ " ");
         }
         System.out.println("");
      }
   }
}

Listing 4

When this code is executed, the following output is generated (the actual program output is in bold).

C:column24>"C:Program FilesJavajdk1.5.0_06binjava" ArrayLists
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24
C:column24>

Even though these types of array initializations are a great way to build testing directly into an application, there are certain applications where arrays can be used in a production situation.

String days[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};

In this case, where the contents of the array are unlikely to change, array initializations come in handy.





Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel