Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Single Variable and a Lot of Variables The declaration int k; float f; reserve one single integer variable called k and one single floating point variable.
Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
For use of IST410 Students only Arrays-1 Arrays. For use of IST410 Students only Arrays-2 Objectives l Declaring arrays l Instantiating arrays l Using.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
1 Arrays b An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
JavaScript, Third Edition
String Escape Sequences
More Arrays Length, constants, and arrays of arrays By Greg Butler.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
A First Book of ANSI C Fourth Edition
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Chapter 8 Arrays and Strings
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Types in Java 8 Primitive Types –byte, short, int, long –float, double –boolean –Char Also some Object types: e.g. “String” But only single items. What.
Data Types and Operations On Data Objective To understand what data types are The need to study data types To differentiate between primitive types and.
Introduction to Java Java Translation Program Structure
IN THE NAME OF ALLAH WHO IS THE MOST BENEFICENT AND MOST MERCIFUL.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Introduction to programming in java Lecture 21 Arrays – Part 1.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Objectives You should be able to describe: One-Dimensional Arrays
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Java Programming: From Problem Analysis to Program Design, 4e
Escape Sequences What if we wanted to print the quote character?
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2: Basic Elements of Java
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Chapter 2: Java Fundamentals
CS 200 Objects and ArrayList
Arrays.
Presentation transcript:

Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be able to create one-dimensional and two-dimensional arrays. To be able to initialize one-dimensional and two-dimensional arrays To be able manipulate one-dimensional numeric arrays. To become familiar with arrays of objects including strings. To be able to apply the concept of arrays to searching and sorting.

Array Introduction Sometimes we have large sets of data that are needed at anytime in a program. If we use a single variable to store each value: a)Each new data value would over write the previous one b)At any point in time only one value could be accounted for c)All the previous values would have been lost int x; X = Using single variable to store data

Array Sometimes we have large sets of data that are needed at anytime in a program. We could use separate variable to represent each value: a)Each new data value would be stored in its own variable b)At any point in time we could accounted for each value c)None of the previous values would ever be lost int x1, x2, x3, x4, x5, x6, x7, x8, x9, x10; x1 = x2 = x3 =x4 = x5 = x6 = x7 = x8 =x9 = x10 = Using individual variable store data

Array Yet a third way would be: a)Store the data contiguously in memory, and b)Use one common name to refer to each location where data is stored. This method gives rise to the concept of array. referenveVariable The array

Array Array: A reserved set of contiguous storage locations to hold a fixed number of homogenous data elements. This means that: 1.All the elements in the array have the same data type The type of values are – any primitive type or any object 2.The size of the array is fixed at compilation time. 3.The size of the array cannot be changed during run time.

Array int x[] The array Array x references 10 (4 bytes) contiguously placed memory locations

Declaring One-Dimensional Array Variables You must declare the array before you can use it. That is, you must specify: The name of the variable that will reference the array, and The type of data that the array is expected to store. The format for declaring a one-dimensional array is as follows: data_type arr[]; or data_type [] arr; Where: data_type is any valid data type (primitive or reference type) arr is the name of the array variable. The name follows the convention for name variables [] is the array symbol. The order of placing the array symbol [] is commutative.

Declaring One-Dimensional Array Variables int numbers[]; String str[]; byte[] someBytes; MotorCar sport[]; char [] consonants; double x[]; Circle [] c1;

Declaring One-Dimensional Array Variables 1.// Declaration does not imply creating the array 2.class ArrayDeclaration 3.{ 4. public static void main(String[] arg) 5. { 6. int numbers[]; 7. System.out.println(numbers); 8. } 9.} Configuration: j2sdk1.4.1_ C:\listing8.1\ArrayDeclaration.java:7: variable numbers might not have been initialized System.out.println(numbers); ^ 1 error

Creating One-Dimensional Array The format for creating a one-dimensional array is as follows: data_type []arr = new data_type [ numberOfCells ]; Or data_type arr [] = new data_type [ numberOfCells ]; int x = new int[10]; String str = new String[10]; byte someBytes = new byte[10]; Circle c1 = new Circle [10]; char consonants = new char [10];

Accessing The Cells Of The Array indeces referenceVariable The cells in a linear (one -dimensional) array are numbered by the operating system. The numbering scheme begins with the first cell labeled cell 0, the second is labeled cell 1, the third is labeled cell 2, and so on. These numbers are referred to as the array indeces.

Alternate Way to Create Array Java provides an alternate way to: Declare Construct, and Explicitly initialize an array. The format is as follows: data_type [] arrayName = { }; When using this construct the individual data must be separated by a comma

Alternate Way to Create Array Create an array of the first 10 even integers beginning with the number two. int evenNumbers[] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}; Create an array of all the vowels. char [] vowels = {‘a’, ‘e’, ‘i’, ‘o’, ‘u’}; Create an array of the character escape sequence. Character escape sequence must also be placed within single quotation marks. char escapes[] = {'\n', '\r', '\b', '\t', '\\', '\"', '\''}; Create an array of menu-items for File, Edit, Insert, and Help. String[] menu = {“File”, “Edit”, “Insert, “Help”};

Manipulating a Linear Array You can assign values directly into the array. The general format of the assignment is as follows: arr[i] = value; Where: arr is the name of the array i is the array index, and value is the data value being assigned to cell. For instance, using the array evenNumbers: evenNumbers [1] = 10; Or read value into array, as in: evenNumbers [6] = GetData.getInt(“Enter an even integer”);

Length of a Linear Array Each array has an instance variable called length. It specifies the size of the array. Givne that arra denotes an one-dimensional array, then the expression: arr.length retrieves the size of the array arr. The following piece of code finds the size of the array menu, and prints each value in the array. int size = menu.length; for (int i = 0; i < size; i++) System.out.println( menu[i] + “\n”);

Convert Integer to Binary Define a class called BinaryDigits that converts an integer value into its binary equivalence. Solution The class receives an integer value. Define a method that decomposes the number into binary digits. Return the binary digits. Algorithm/rule for converting an integer into binary. Divide the original number by 2 and Record the remainder. Set the number to the quotient Repeat the process until the new number becomes zero. When the process stops, rewrite the result starting with the last value first.

Convert Integer to Binary Let us use the number 53 to test this algorithm. 53/2 = 26 remainder 1 26/2 = 13 remainder 0 13/2 = 6 remainder 1 6/2 = 3 remainder 0 3/2 = 1 remainder 1 2/2 = 0 remainder 1 Hence the binary equivalence of 53 is

Convert Integer to Binary Principal variables are: The number An array to store the remainder An integer value to which to set the size of the array An indexing variable for both the loop and the array. Principal methods are: The constructor An instance method to implement the above algorithm We may code the toString method to return the result

Convert Integer to Binary 1.class BinaryDigits 2.{ 3. int binary[], x, theNumber, index; 4. static final int MAX = 32; 5. BinaryDigits(int x) 6. { 7. binary = new int[MAX]; 8. this.x = x; 9. index = 0; 10. theNumber = x; 11. } 15. void makeBinaryDigits() 16. { 17. // … 25. } 26. public String toString() 27. { 28. // ……. 33. } 34.}

Convert Integer to Binary 15.void makeBinaryDigits() 16.{ 17. int remainder; 18. while (x > 0 && index < MAX) 19. { 20. remainder = x % 2; 21. binary[index] = remainder; 22. x = x/ 2; 23. index = index + 1; 24. } 25.}

Convert Integer to Binary 26.public String toString() 27.{ 28. String s = "The number " + theNumber + " = "; 29. int index = this.index - 1; 30. for (int i = index; i >= 0; i-- ) 31. s = s + binary[i] + " "; 32. return s + " in binary"; 33.}

Convert Integer to Binary 1.class TestBinary 2.{ 3. public static void main(String[] arg) 4. { 5. BbinaryDigits b = new BinaryDigits(53); 6. b.makeBinaryDigits(); 7. System.out.println(b); 8. } 9.}