Java Arrays.pdf

Document Details

Uploaded by Deleted User

Tags

java programming arrays data structures

Full Transcript

7 Java Arrays Introduction to Programming 1 1 Objectives 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 Java Arrays Introduction to Programming 1 1 Objectives 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 Introduction to Programming 1 2 Introduction to Arrays Suppose 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. Introduction to Programming 1 3 Introduction to Arrays 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 data type, in a contiguous block of memory, divided into a number of slots. Introduction to Programming 1 4 Declaring Arrays To declare an array, write the data type, followed by a set of square brackets[], followed by the identifier name. For example, int []ages; or int ages[]; Introduction to Programming 1 5 Array Instantiation After declaring, we must create the array and specify its length with a constructor statement. Definitions: – Instantiation In Java, this means creation – Constructor In order to instantiate an object, we need to use a constructor for this. A constructor is a method that is called to create a certain object. We will cover more about instantiating objects and constructors later. Introduction to Programming 1 6 Array Instantiation To instantiate (or create) an array, write the new keyword, followed by the square brackets containing the number of elements you want the array to have. For example, //declaration int ages[]; //instantiate object ages = new int; or, can also be written as, //declare and instantiate object int ages[] = new int; Introduction to Programming 1 7 Array Instantiation Introduction to Programming 1 8 Array Instantiation You can also instantiate an array by directly initializing it with data. For example, int arr[] = {1, 2, 3, 4, 5}; This statement declares and instantiates an array of integers with five elements (initialized to the values 1, 2, 3, 4, and 5). Introduction to Programming 1 9 Sample Program 1 //creates an array of boolean variables with identifier 2 //results. This array contains 4 elements that are 3 //initialized to values {true, false, true, false} 4 5 boolean results[] = { true, false, true, false }; 6 7 //creates an array of 4 double variables initialized 8 //to the values {100, 90, 80, 75}; 9 10 double []grades = {100, 90, 80, 75}; 11 12 //creates an array of Strings with identifier days and 13 //initialized. This array contains 7 elements 14 15 String days[] = { “Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat”, “Sun”}; Introduction to Programming 1 10 Accessing an Array Element To access an array element, or a part of the array, you use a number called an index or a subscript. index number or subscript – assigned to each member of the array, to allow the program to access an individual member of the array. – begins with zero and progress sequentially by whole numbers to the end of the array. – NOTE: Elements inside your array are from 0 to (sizeOfArray-1). Introduction to Programming 1 11 Accessing an Array Element For example, given the array we declared a while ago, we have //assigns 10 to the first element in the array ages = 10; //prints the last element in the array System.out.print(ages); Introduction to Programming 1 12 Accessing an Array Element NOTE: – once an array is declared and constructed, the stored value of each member of the array will be initialized to zero for number data. – for reference data types such as Strings, they are NOT initialized to blanks or an empty string “”. Therefore, you must populate the String arrays explicitly. Introduction to Programming 1 13 Accessing an Array Element The following is a sample code on how to print all the elements in the array. This uses a for loop, so our code is shorter. 1 public class ArraySample{ 2 public static void main( String[] args ){ 3 int[] ages = new int; 4 for( int i=0; i

Use Quizgecko on...
Browser
Browser