{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

chapter_15_arraylists_javabook - These are sample pages...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CHAPTER 15 MORE STANDARD JAVA TYPES The two preceding chapters have introduced standard Java classes. This chapter continues with the same theme. As we have now studied the basic features of Java, there are only the huge number of standard classes and other types that are left to be learned. Fortunately, you do not have know them all. You can study them gradually, as is necessary. I hope that after this book has introduced some of the standard classes, you will be able to learn the other standard classes from the electronic Java documentation and other sources. ArrayList is a standard class that is useful in applications in which dynamic arrays are needed. A dynamic array is such that its length (or size) is not fixed. Instead, the array length may increase or decrease while the array is used in a program. Class ArrayList , the first subject of this chapter, provides methods which automatically increase or decrease the length of an array in operations that insert or remove new objects to/from an array. Interfaces, and especially standard interfaces like Comparable , is the second subject of this chapter. Interfaces specify methods. A class can implement an interface, which means that the class is equipped with certain methods. The third subject of this chapter is the standard class GregorianCalendar , the official Java type for handling information related to dates and time. GregorianCalendar is a subclass of a class named Calendar . You will be shown how class GregorianCalendar can be used instead of the Date class which was introduced in Chapter 11. These are sample pages from Kari Laitinen’s book A Natural Introduction to Computer Programming with Java . For more information, please visit http://www.naturalprogramming.com/javabook.html
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
502 Chapter 15: More standard Java types 15.1 ArrayList class We have learned a long time ago that an array of type int[] is a data structure that can contain a certain number of data items of type int . Similarly, an array whose type is, say, President[] is an array that can contain a certain number of references to objects of type President . These traditional Java arrays are created with statements like int[] array_of_integers = new int[ 50 ] ; President[] president_table = new President[ 80 ] ; and individual array elements can be accessed by giving an index expression inside brack- ets: array_of_integers[ integer_index ] = 77 ; president_table[ 0 ] = new President( ... ) ; When a traditional array is created, the length of the array is specified by the value that is given in brackets. The length of an array, which can be read from the array field length , is the number of array elements. For example, the above array_of_integers has 50 array elements to store values of type int , and its length is thus 50. A shortcoming of a traditional array is that its length cannot be changed after the array has been created. A traditional array is efficient, which means that not much comput- ing time is required to read and write the array elements, but in some programs the fixed length of an array causes problems, or at least makes programming difficult. For this rea-
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern