Objectives Introduction to arrays Declaring Arrays

J.E.D.I 7 Java Arrays

7.1 Objectives

In this section, we will be discussing about Java Arrays. First, we are going to define what arrays are, and then we are going to discuss on how to declare and use them. At the end of the lesson, the student should be able to: • Declare and create arrays • Access array elements • Determine the number of elements in an array • Declare and create multidimensional arrays

7.2 Introduction to arrays

In the previous sections, we have discussed on how to declare different variables using the primitive data types. In declaring variables, we often use a unique identifier or name and a datatype. In order to use the variable, we call it by its identifier name. For example, we have here three variables of type int with different identifiers for each variable. int number1; int number2; int number3; number1 = 1; number2 = 2; number3 = 3; As you can see, it seems like a tedious task in order to just initialize and use the variables especially if they are used for the same purpose. In Java and other programming languages, there is one capability wherein we can use one variable to store a list of data and manipulate them more efficiently. This type of variable is called an array. An array stores multiple data items of the same datatype, in a contiguous block of memory, divided into a number of slots. Think of an array as a stretched variable – a location that still has one identifier name, but can hold more than one value. Introduction to Programming I 114 Figure 7.1: Example of an Integer Array J.E.D.I

7.3 Declaring Arrays

Arrays must be declared like all variables. When declaring an array, you list the data type, followed by a set of square brackets[], followed by the identifier name. For example, int []ages; or you can place the brackets after the identifier. For example, int ages[]; After declaring, we must create the array and specify its length with a constructor statement. This process in Java is called instantiation the Java word for creates. In order to instantiate an object, we need to use a constructor for this. We will cover more about instantiating objects and constructors later. Take note, that the size of an array cannot be changed once youve initialized it. For example, declaration int ages[]; instantiate object ages = new int[100]; or, can also be written as, declare and instantiate object int ages[] = new int[100]; In the example, the declaration tells the Java Compiler that the identifier ages will be used as the name of an array containing integers, and to create or instantiate a new array containing 100 elements. Instead of using the new keyword to instantiate an array, you can also automatically declare, construct and assign values at once. Introduction to Programming I 115 Figure 7.2: Instantiating Arrays J.E.D.I Examples are, creates an array of boolean variables with ientifier results. This array contains 4 elements that are initialized to values {true, false, true, false} boolean results[] ={ true, false, true, false }; creates an array of 4 double variables initialized to the values {100, 90, 80, 75}; double []grades = {100, 90, 80, 75}; creates an array of Strings with identifier days and initialized. This array contains 7 elements String days[] = { “Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat”, “Sun”}; Introduction to Programming I 116 J.E.D.I

7.4 Accessing an array element