Introduction to arrays Array One dimenstional array.

Slides:



Advertisements
Similar presentations
1 Arrays, Strings and Collections [1] Rajkumar Buyya Grid Computing and Distributed Systems (GRIDS) Laboratory Dept. of Computer Science and Software Engineering.
Advertisements

Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Introduction to Programming
Introduction to Programming
Chapter 7: Arrays In this chapter, you will learn about
Chapter 8: Arrays.
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
1 Arrays An array is a special kind of object that is used to store a collection of data. The data stored in an array must all be of the same type, whether.
1 Review Quisioner Kendala: Kurang paham materi. Praktikum Pengaruh teman.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
CS 141 Computer Programming 1 1 Arrays. Outline  Introduction  Arrays  Declaring Arrays  Examples Using Arrays  Sorting Arrays  Multiple-Subscripted.
Introduction to Arrays Chapter What is an array? An array is an ordered collection that stores many elements of the same type within one variable.
Java POWERED BY: ARVIND DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING RADHA GOVIND GROUP OF INSTITUTIONS,MEERUT.
Introduction to Programming G51PRG University of Nottingham Revision 1
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Loops.
1 Arrays Chapter 9. 2 Outline  The array structure (Section 9.1)  Array declaration  Array initialization  Array subscripts  Sequential access to.
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 Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
8. Arrays 8.1 Using Arrays 8.2 Reference versus Value Again 8.3 Passing Array Arguments and Returning Arrays 8.4 An Example: Simulating Rolls of the Dice.
Arrays Clark Savage Turner, J.D., Ph.D. Copyright © 2000 by C Scheftic. All rights reserved. These notes do rely heavily.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
1 One-Dimensional (1-D) Array Overview l Why do we need 1-D array l 1-D array declaration and Initialization l Accessing elements of a 1-D array l Passing.
1 Arrays b An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
1 Modelling 1-D Array Overview l Why do we need 1-D array l 1-D array declaration and Initialization l Accessing elements of a 1-D array l Passing Array.
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.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Arrays in Java Selim Aksoy Bilkent University Department of Computer Engineering
COMP 14 Introduction to Programming Miguel A. Otaduy June 1, 2004.
CS0007: Introduction to Computer Programming Introduction to Arrays.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
1 Dr. Seuss again: "Too Many Daves"  Did I ever tell you that Mrs. McCave Had twenty-three sons, and she named them all Dave?  Well, she did. And that.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
CSE 501N Fall ‘09 08: Arrays 22 September 2009 Nicholas Leidenfrost.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
1 Objects for Organizing Data -- Introduction zAs our programs get more sophisticated, we need assistance organizing large amounts of data zChapter 6 focuses.
Chapter 5: ARRAYS ARRAYS. Why Do We Need Arrays? Java Programming: From Problem Analysis to Program Design, 4e 2  We want to write a Java program that.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Week 6 - Friday.  What did we talk about last time?  Loop examples.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
Introduction to programming in java Lecture 22 Arrays – Part 2 and Assignment No. 3.
Chapter 7 Arrays…. 7-2 Arrays An array is an ordered list of values An array of size N is indexed from.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Lecture 5 array declaration and instantiation array reference
Arrays of Objects October 9, 2006 ComS 207: Programming I (in Java)
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
CSC 211 Java I for loops and arrays.
Sections 10.1 – 10.4 Introduction to Arrays
Arrays An Array is an ordered collection of variables
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.
Introduction To Programming Information Technology , 1’st Semester
Arrays of Objects October 8, 2007 ComS 207: Programming I (in Java)
Arrays.
Presentation transcript:

Introduction to arrays Array One dimenstional array

outlines What is an array Why arrays How to declare and initialise an array How to use for loops to process arrays Array index out of bounds exceptions

Array definition An array is the memory of the computer used to store lots of data items of the same types. An array is an ordered list of value

Array index marks index The array marks holds 10 marks: The 1 st mark is indexed by 0 The last mark is indexed by 9(10-1)

Array index with N values Nmarks index The array Nmarks array holds N marks: The 1 st mark is indexed by 0 The last mark is indexed by (N-1) N-1

How to reference a value in an array? Nmarks index A particular value in an array is referenced using the array name followed by the index in brackets System.out.println(Nmarks[0]); will print 79, Nmarks[9] 99 N-1

How to reference a value in an array? marks index marks[0] refers to 79 where as marks[9] refers to 91 A particular value in an array is referenced using the array name followed by the index in brackets

Array declaration We first declare a as an array of integers. – int [] a ; We the give it a space in the memory to hold 10 items(integes). – a new in[10];

Array declaration (cont) We can also declare and assign a memory space at the same time – int [] a = new int[10];

Array declaration and initialisation We can also declare and assign a memory space at the same time – int [] a = {10, 5, 6, 22, 11, 13, 15, 81, 8,26} ;

Array declaration (Cont) An array can hold only objects of the same type specified in the declaration step. Int [] intarr = new int[5]; String [] starr= new String[5]; Char [] charr= new Char[5]; Doucle [] darr= new double[5]; Boolean [] barr= new boolean[5];

Array declaration Initializer Lists (cont) An initializer list can be used to instantiate and initialize an array in one step The values are delimited by braces and separated by commas Examples: – int[] units = {147, 323, 89, 933, 540, 269, 97, 114, 298, 476}; – char[] letterGrades = {'A', 'B', 'C', 'D', 'F'}; – String [] names= {xxxx, yyy, xx, tt,dddddddd};

Array declaration Initializer Lists (cont) Note that when an initializer list is used: – the new operator is not used – no size value is specified The size of the array is determined by the number of items in the initializer list An initializer list can only be used in the declaration of an array

Array declaration (cont) int[] x, y, z; – identifies that x, y and z are all arrays. int x[], y, z; – identifies that only x is an array, y and z are simply integers.

Why arrays? Imagine you want to write a program that asks the user to enter in 50 student midterm marks. The program would then display each grade with the students ID number and then the average for that assignment. Write a program, MarksArray.java to achieve this?

Why arrays? (cont) Imagine you want to write a program that asks the user to enter in 50 student midterm marks. The program would then display each grade with the students ID number and then the average for that assignment in a nice report. How would you do it? One way would be to provide 100 variables: – int mark1, mark2, mark3, (until 50) – int ID1, ID2, ID3, (until 50) The array provides for a way to create only two variables: – int marks[] = new int[50]; – int IdNumbers[] = new int[50]; This much easier then declaring 100 variables.

Array length Int [] ar = new int [10]; a.length = 10 The values are a[0] to a[a.length-1]

Primes.java public class Primes { public static void main (String[] args) { int[] primes = {2, 3, 5, 7, 11, 13, 17, 19,23}; System.out.println ("Array length: " + primes.length); System.out.println ("The first few prime numbers are:"); for (int prime = 0; prime < primes.length; prime++) System.out.print (primes[prime] + " "); System.out.println (); }

Array index out of bounds Once an array is created, it has a fixed size An index used in an array reference must specify a valid element That is, the index value must be in bounds (0 to N-1) The Java interpreter will throw an exception if an array index is out of bounds This is called automatic bounds checking

Example Int [] ar = new int [10]; Ar[10]=86; It prints an error Exception in thread main java.lang.ArrayIndexOutOfBoundException:86

Example // StudentArray.java: store integers in arrays and access public class StudentArray { public static void main(String[ ] args) { int[] students = {55, 69, 70, 30, 80}; System.out.println("Array Length = " + students.length); System.out.println("Values Stored in Array:"); for ( int i=0; i < students.length; i+ +) System.out.println(students[ i] ); }

Example Write a java program that stores the first hundred positive integers in an array and prints them in a reverse order.

Example Write a java program that stores that allows the user to enter 10 positive integers, stores them in an array. Then prints how many even number were enter if any.

Example Write a program that allows the user to enter 10 integers stores them in an array and search for largest and the smallest in the array.

Example Write a java program that uses two array of length 10: arrID stores the students id and arrMark stores the students mark. For each students the program should print the student and id and its mark as follows: – Student idMark –

Example Write a numbera program that generates 100 random integers between 0 to 99 and stores them any array and prints the largest, smallest and the average.

Example Write a program where the user types in a number of Strings which are stored in an array of Strings and then the program prints out all the longest Strings entered by the user.

Example Write a program where the user types in a number of Strings stored in an array of Strings and then the program prints out the string that has the most occurrences of the character a.