5-Aug-2002cse142-16-Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1

Slides:



Advertisements
Similar presentations
11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
Advertisements

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.
Written by: Dr. JJ Shepherd
Chapter 11 Arrays. Introduction Array: An ordered collection of values with two distinguishing characters: – Ordered and fixed length – Homogeneous. Every.
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.
Chapter 10 Introduction to Arrays
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Arrays.
Strings and Arrays The objectives of this chapter are:  To discuss the String class and some of its methods  To discuss the creation and use of 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.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
© The McGraw-Hill Companies, 2006 Chapter 5 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.
1 Chapter 2 Introductory Programs. 2 Getting started To create and run a Java program –Create a text file with a.java extension for the source code. For.
Alice in Action with Java
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API.
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 4: Strings & Arrays Sanjay Goel.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Java Unit 9: Arrays Declaring and Processing Arrays.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
ArrayList, Multidimensional Arrays
Chapter 11 Arrays Continued
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.
© 2007 Lawrenceville Press Slide 1 Chapter 10 Arrays  Can store many of the same kind of data together  Allows a collection of related values to be stored.
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.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
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.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Everything is an object (CH-2) Manipulating Objects with References. Manipulating Objects with References. String s; String s = “IS2550” String s = new.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
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.
CSE 1201 Object Oriented Programming ArrayList 1.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Written by: Dr. JJ Shepherd
Arrays Chapter 6. Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
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];
JAC444: Intro to Java Arrays and Vectors Tim McKenna
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter VII: Arrays.
CSE 413, Autumn 2002 Programming Languages
Sections 10.1 – 10.4 Introduction to Arrays
Sixth Lecture ArrayList Abstract Class and Interface
Lecture 5 D&D Chapter 6 Arrays and ArrayLists Date.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Chapter 7 Part 1 Edited by JJ Shepherd
Chapter 8: Collections: Arrays
Can store many of the same kind of data together
Object Oriented Programming in java
python.reset() Also moving to a more reasonable room (CSE 403)
slides created by Ethan Apter
Single-Dimensional Arrays chapter6
Arrays in Java.
Java Programming Language
Review: libraries and packages
Arrays.
Presentation transcript:

5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1

5-Aug-2002cse Arrays © 2002 University of Washington2 Readings and References Reading »Section 22.1, An Introduction to Programming and Object Oriented Design using Java, by Niño and Hosch »Chapters 18 and 19, Introduction to Programming in Java, Dugan Other References »Section Arrays of the Java tutorial »

5-Aug-2002cse Arrays © 2002 University of Washington3 Arrays Java (and many other languages) include arrays as the most basic kind of collection. »Simple, ordered collections, similar to ArrayLists. »Special syntax for declaring values of array type. »Special syntax for accessing elements by position. Unlike ArrayLists: »The size is fixed when the array is created. »Can specify the type of the elements of arrays.

5-Aug-2002cse Arrays © 2002 University of Washington4 Array Example public class ArraySample { public ArraySample() { names = new String[3]; names[0] = "Sally"; names[1] = "Splat"; names[2] = "Google"; for (int i=0; i<names.length; i++) { System.out.println("Name "+i+" is "+names[i]); } String[] names; }

5-Aug-2002cse Arrays © 2002 University of Washington5 Array Example names String[ ] index 0 index 1 index 2 String "Sally" String "Splat" String "Google" length : 3

5-Aug-2002cse Arrays © 2002 University of Washington6 Java Array Object Arrays are objects! They... »Must be instantiated with new unless immediately initialized »Can contain Object references or primitive types »Have class members (length, clone(),…) »Have zero-based indexes »Throw an exception if bounds are exceeded

5-Aug-2002cse Arrays © 2002 University of Washington7 Array Declaration and Creation Array have special type and syntax: [ ] = new [ ]; Arrays can only hold elements of the specified type. »Unlike ArrayList, element type can be int, double, etc. »type can be Object, in which case very similar to ArrayList is any positive integer expression Elements of newly created arrays are initialized »but generally you should provide explicit initialization Arrays have an instance variable that stores the length.length

5-Aug-2002cse Arrays © 2002 University of Washington8 Declaring and Allocating Arrays Declare an Array of ten String references String[] myArray = new String[10]; Declare an array and initialize elements »the compiler counts the number of elements in this case String[] myArray = { “Java”,”is”,”cool”}; Declare, initialize, and use an array »this is an "anonymous" array boolean okay = doLimitCheck(x,new int[] {1,100});

5-Aug-2002cse Arrays © 2002 University of Washington9 Array Element Access Access an array element using the array name and position: [ ] Details: » is an integer expression. »Positions count from zero, as with ArrayLists. »Type of result is the element type of the array Can update an array element by assigning to it: [ ] = ; »Like ArrayList's set method

5-Aug-2002cse Arrays © 2002 University of Washington10 Looping Over Array Contents The length attribute makes looping over Array objects easy: for (index=0; index<myArray.length; index++) { System.out.println(myArray[index]); } The length attribute is a read-only value »You can't change the size of the array after it has been created

5-Aug-2002cse Arrays © 2002 University of Washington11 Passing Array Objects to Methods You must declare that a method parameter is an Array: public static void main(String[] args) Arrays are objects and so you are passing a reference when you call a method with an array »This means array contents can be changed by methods »This may be what you want, but if not, you need to make sure that other methods only get a copy of your array and the elements in it

5-Aug-2002cse Arrays © 2002 University of Washington12 Array Summary Arrays are the fundamental low-level collection type built in to the Java language. »Also found in essentially all programming languages Size fixed when created Indexed access to elements Used to implement higher-level, richer container types »ArrayList for example »More convenient, less error-prone for users

5-Aug-2002cse Arrays © 2002 University of Washington13 The Arrays Class There is also a class called java.util.Arrays »Note the capital A, this is a class name »part of package java.util »utility functions for using arrays search sort initialize »These are static methods so they exist and can be used without creating an object first

5-Aug-2002cse Arrays © 2002 University of Washington14 The Collections Class There is also a class called java.util.Collections »utility functions for using classes that implement the Collection interface »This class consists exclusively of static methods that operate on or return collections. It contains polymorphic algorithms that operate on collections, "wrappers", which return a new collection backed by a specified collection, and a few other odds and ends. »These are static methods so they exist and can be used without creating an object first

5-Aug-2002cse Arrays © 2002 University of Washington15 Useful methods in Collections class static void sort(List list) »Sorts the specified list into ascending order, according to the natural ordering of its elements. »"natural order" is defined when you implement the interface Comparable static void sort(List list, Comparator c) »Sorts the specified list according to the order induced by the specified comparator »Comparator lets you define several different orders