Skip to Content
Java Cookbook
book

Java Cookbook

by Ian F. Darwin
June 2001
Intermediate to advanced
888 pages
21h 1m
English
O'Reilly Media, Inc.
Content preview from Java Cookbook

Like an Array, but More Dynamic

Problem

You don’t want to worry about storage reallocation; you want a standard class to handle it for you.

Solution

Use a Vector. Or, in Java 2, an ArrayList.

Discussion

A Vector is just a standard class that encapsulates the functionality of an array but allows it to expand automatically. You can just keep on adding things to it, and each addition will behave the same. If you watch really closely you might notice a brief extra pause once in a while when adding objects, as Vector reallocates and copies. But you don’t have to think about it.

However, because Vector is a class and isn’t part of the syntax of Java, you can’t use Java’s array syntax; you must use methods to access the Vector data. There are methods to add objects, retrieve objects, find objects, and tell you how big the Vector is and how big it can become without having to reallocate. Like those of all the collection classes in java.util, Vector’s storing and retrieval methods are defined in terms of java.lang.Object. But since Object is the ancestor of every defined type, you can store objects of any type in a Vector (or any collection), and cast it when retrieving it. If you need to store a small number of built-ins (like int, float, etc.) into a collection containing other data, use the appropriate wrapper class (see the Introduction to Chapter 5). To store booleans, either use a java.util.BitSet (see the online documentation) or the Boolean wrapper class.

Table 7-1 shows some of the ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Java I/O

Java I/O

Elliotte Rusty Harold
Practical Cloud-Native Java Development with MicroProfile

Practical Cloud-Native Java Development with MicroProfile

Emily Jiang, Andrew McCright, John Alcorn, David Chan, Alasdair Nottingham

Publisher Resources

ISBN: 0596001703Catalog PageErrata