Programming with Collections Collections in Java Using Arrays Week 9.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Arrays.
Grouping objects Arrays and for loops. Fixed-size collections Sometimes the maximum collection size can be pre-determined. Programming languages usually.
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.
Grouping Objects Arrays and for loops. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Fixed-Size Collections.
 2003 Prentice Hall, Inc. All rights reserved. 7.1 Introduction Arrays –Data structures which reference one or more value –All items must have same data.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
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.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
Grouping objects Collections and iterators. 04/11/2004Lecture 4: Grouping Objects2 Main concepts to be covered Collections Loops Iterators Arrays.
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
Arrays.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 2.0.
Grouping objects Arrays, Collections and Iterators 1.0.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
COMP102 Lab 081 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
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 and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Chapter 8: Arrays.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
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.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Arrays Array – Group of contiguous memory locations Each memory location has same name Each memory location has same type.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Grouping objects Collections and iterators Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Arrays Arrays in C++ An array is a data structure which allows a collective name to be given to a group of elements which all have.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
+ Structures and Unions. + Introduction We have seen that arrays can be used to represent a group of data items that belong to the same type, such as.
Arrays Chapter 13 How to do the following with a one dimensional array: Declare it, use an index.
1 One Dimensional Arrays Chapter 11 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores :
Arrays Adapted from materials created by Dr. Donald Bell, Cal Poly 2000 (updated February 2004)
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Grouping objects Arrays. 2 Fixed-size collections The maximum collection size may be pre-determined with an upper limit Array is an fixed-size collection.
Arrays.
Introducing Arrays. Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
 2005 Pearson Education, Inc. All rights reserved Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Fixed-sized collections Introduction to arrays 6.0.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Arrays Collections of data Winter 2004CS-1010 Dr. Mark L. Hornick 1.
Data Storage So far variables have been able to store only one value at a time. What do you do if you have many similar values that all need to be stored?
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Objects First with Java CITS1001
Introduction To Programming Information Technology , 1’st Semester
Object Oriented Programming in java
Arrays .
MSIS 655 Advanced Business Applications Programming
Collections and iterators
Collections and iterators
Presentation transcript:

Programming with Collections Collections in Java Using Arrays Week 9

 Arrays  Using the For Loop with Arrays Programming with Collections CONCEPTS COVERED THIS WEEK

Programming with Collections FIXED-SIZE COLLECTIONS  Sometimes the maximum collection size can be pre-determined.  Programming languages usually offer a special fixed-size collection type: an array.  Java arrays can store either objects or primitive-type values (e.g. int, float, etc…)  Arrays use a special syntax.

What is an array? A special type of data structure which consists of a group of items of the same type, e.g. int, double, String etc. We can manipulate the whole group as a single entity and we can use any single member of that group (called an array element) independently. Each element is located by an INDEX or SUBSCRIPT pointing to the appropriate ‘cell’ number.

Using Arrays Declaring & initialising an array called numbers that has 10 integer elements: int[ ] numbers; numbers = new int [10]; This tells the system to create 10 linked integer memory locations that can be accessed by the name numbers[..]

The numbers array numbers The statement: numbers[5] = 8; puts 8 in element

Note The subscripts (elements) are numbered from 0 to 9, so subscript number 5 is the sixth one along. Array subscripts ALWAYS start at 0. Arrays have square brackets [ ]. Uninitialised numeric array elements take the value 0 by default.

Creating an array Choose any legal variable name Key in [ ] square brackets before (left) or after (right) of the name. int ages[ ]; String[ ] names; ages = new int [6]; names = new String [5]; Numbers in [ ] are indices - they tell how many items BUT can also be which element is referred to

Using Arrays Square-bracket notation is used to access an array element: c[5] Elements are used like ordinary variables. –On the left of an assignment: c[5] = 8; –In an expression: int adjusted = c[5] – 3; c[5]++;

Another example int c[]; c = new int[12]; //to ‘load’ each element we could use 12 separate statements c[0] = -45; c[1] = 6; c[2] = 0; c[3] = 72; c[4] = 1543; c[5] = -89; c[6] = 0; c[7] = 62; c[8] = -3; c[9] = 1; c[10] = 6453; c[11] = 78; Alternatively, we could initialise (‘populate’) the array when we declare it: int c[] = {-45, 6, 0, 72, 1543, -89, 0, 62, -3, 1, 6453, 78};

Arrays and for loops Suppose we wanted to display all the elements of array c[], we could write 12 System.out.print statements OR use a ‘for’ loop: for (int i = 0; i < c.length; i++) System.out.print ( c[i] + “ ” ); c.length returns the number of elements in the array, 12 in this case

Arrays and for loops To add all the elements of array c[] and put the answer in sum: int sum = 0; for (int i = 0; i < c.length; i++) sum += c[i]; System.out.println (“The sum is ” + sum);

Arrays & for loops //To find the largest element in c int largestValue = c[0]; int indexOfLargest = 0; for (int i = 0; i < c.length; i++) { if (c[i] > largestValue) { largestValue = c[i]; indexOfLargest = i; }

Good Programming Tip It’s better to declare the size of the array independently of the array declaration, e.g. int ARRAY_SIZE = 12; int c[]; c = new int [ARRAY_SIZE]; Then, to change the size, you only need to change one line.

Arrays Summary Arrays are appropriate where a fixed- size collection of data items is required. Arrays use special syntax. The subscript always starts at 0. For loops are often used to iterate over arrays.

Common Array Tasks Create Display contents Search Sum (add all numeric elements) Sort

Programming with Collections REVIEW OF ARRAYS  Arrays are appropriate where a fixed-size collection is required.  Arrays use special syntax.  For loops offer an alternative to while loops when the number of repetitions is known.  For loops are often used to iterate over arrays.

Programming with Collections ARRAYS vs ARRAYLISTS  An array has to be given its size at the time it is created. An array list does not have a pre- determined size.  To place an item in an array you need to place it in a specific element location. With an array list you just use the add method.  An array can store either objects or primitive data types but an array list can only store objects.