Java: Data Sets Using Arrays and Collections - dummies

Java: Data Sets Using Arrays and Collections

By John Paul Mueller

Just about everyone makes lists. Java provides lists, too, in the form of arrays. Lists are a natural way of keeping track of data of various sorts. The easiest way to think of arrays is as electronic lists that help you manage the data in your application.

Not every list you create has just one dimension. For example, you might end up creating a table that not only tells what to do, but when and where to do it. Tables are two-dimensional lists — they’re composed of rows and columns.

People naturally create tables when they need to track data that’s too complex for a simple list. Likewise, Java provides you with access to two-dimensional arrays that work much like tables do.

Sometimes you need even more than two dimensions. For example, when you try to decide how to manage stock in a warehouse, you use a three-dimensional list to do it — height, width, and depth. Woodworkers commonly use three-dimensional lists, too. In fact, humans use multidimensional lists all the time without really thinking about it. Java can meet your needs in this regard as well.

Arrays could meet every listing need. However, some forms of specialty lists are so common that Java provides special objects for them. For example, when you’re making pancakes, you often create a stack of them. When you finish the stack, you take the last item you put on it first and then move down toward the first item.

Likewise, we’re all familiar with lines called queues at the bank and shopping center, where the first person in line is also the first person to leave it. Java provides an assortment of special list types that are really offshoots of the array, but provide special logic for managing the list with greater ease: stack, queue, and deque (double-ended queue).

Finally, Java can use a truly specialized form of array with intelligence called a map. Most people think about this sort of object as a collection. When you have a DVD collection, you think about more than a simple stack of disks. The content of each DVD takes on special importance and you often categorize DVDs by type and list them in order of title.

It’s the use of these database-like qualities that changes an array from a simple list to a type of collection called a map. The following sections describe all these forms of Java lists. In addition, a new method of documentation for complex types is called annotations that you’ll find very helpful as your applications become more complex.

Here are some terms you need to know about Java arrays:

  • An array is a kind of electronic list. It’s a grouping of individual data values — normally of the same type — that you treat as a single entity. You use an array as a sort of container for managing a list of data.

  • A collection is a list of complex items that you manage in the same way that you would a database. Java provides the Map object for this purpose. The list isn’t structured in a concrete fashion — you can reorder it as needed and process it in a more comprehensive way than other Java types.

  • A stack is a special type of array that forces you to remove the last item you added to the list first. This is a Last In, First Out (LIFO) structure that acts much like a stack of pancakes, where you take the topmost pancake from the stack first when you’re having breakfast.

  • A queue is a special type of array that forces you to remove the first item you added to the list first. This is a First In, First Out (FIFO) structure that acts much like a line at the bank. The first person in line is the first person served.

  • A deque is a special kind of array that lets you add or remove items from either end of the list. However, you can’t access items in the middle of the list. A deque can act as either a stack or a queue depending on the need at the time. It lets a developer create a list where items that a program can’t act on now are put back into the list for later processing.