Java: How to Program Arrays and ArrayLists Summary Yingcai Xiao.

Slides:



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

Two Dimensional Arrays and ArrayList
ArrayList Difference of Array and ArrayList [Sample code] TestArrayList.java.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
CS102 A Wide Array of Possibilities CS 102 Java’s Central Casting.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Chapter 5 Arrays and Vectors An array allows you to group data items together in a structure that is processed via an index. They allow you to process.
Arrays Chapter 6 Chapter 6.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 10 Arrays. Topics Declaring and instantiating arrays Array element access Arrays of objects Arrays as method parameters Arrays as return values.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
More Arrays Length, constants, and arrays of arrays By Greg Butler.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Java Unit 9: Arrays Declaring and Processing Arrays.
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 9: Advanced Array Concepts
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
C Static Arrays Pepper. What is an array? Memory locations – same type – next to each other (contiguous) – Same name – Indexed by position number of type.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
JAVA Array 8-1 Outline  Extra material  Array of Objects  enhanced-for Loop  Class Array  Passing Arrays as Arguments to Methods  Returning Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
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.
Objectives - 11  We will work with processing Arrays.  Objectives:  Describe the concept of an array and its benefits.  Define the terms index, traverse,
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about 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.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
Scope When we create variables and functions, they are limited in where they are visible and where they can be referenced For the most part, the identifiers.
Weeks 5-6 Pointers and Arrays Basic pointer type Pointers and Arrays Address arithmetic Pointer Arrays User-defined data types Structures Unions Pointers.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
Chapter 10 Arrays. Learning Java through Alice © Daly and Wrigley Objectives Declare and use arrays in programs. Access array elements within an array.
1 Arrays and Methods Java always passes arguments by value – that is a copy of the value is made in the called method and this is modified in the method.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Arrays Chapter 7. 2 Declaring and Creating Arrays Recall that an array is a collection of elements all of the _____________ Array objects in Java must.
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
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Array and String.
COS 312 DAY 12 Tony Gauvin. Ch 1 -2 Agenda Questions? First Progress report due Assignment corrected 1 MIA – 2 A’s, 2 B’s, 1 D, 1 F and 1 MIA – Code\pig.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
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.
 2005 Pearson Education, Inc. All rights reserved Arrays.
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Chapter 8 Arrays and the ArrayList Class Multi-Dimensional Arrays.
Arrays Chapter 7.
Arrays Chapter 7.
Chapter-7 part3 Arrays Two-Dimensional Arrays The ArrayList Class.
Chapter 8 Slides from GaddisText
Object Oriented Programming in java
Arrays Chapter 7.
1D Arrays and Lots of Brackets
Object Oriented Programming
Ps Module 7 – Part II 2D Arrays and LISTS 5/26/2019 CSE 1321 Module 7.
Presentation transcript:

Java: How to Program Arrays and ArrayLists Summary Yingcai Xiao

Arrays: Basics  What is it? A reference to a consecutive memory of the same data type.  How to create it? int[] c; // declare the array variable c = new int[ 12 ]; // creates the array  How to use it? for(int i=0; i<c.length; i++) c[i] = i*i*i; for(int =0; i<c.length; i++} System.out.println(“The cube of %d is %d.”, i, c[i]);  Example? Figures 7.7, 7.10, 7.12, 7.14, 7.18,  Read and write down what the programs do (specifications).  Write the code yourself based on the specifications. Test your code.

Arrays: things need to know  Indexed from 0, the largest index should be [length – 1]. Bounds checking by Java.  Initializer list. int[] n = { 10, 20, 30, 40, 50 };  An easy way of reading array elements. System.out.println(“Values of array are:”); for(int v : n) System.out.println(“%d”, v);  Multidimensional arrays int[][] b = new int[ 2 ][ ]; // create 2 rows b[ 0 ] = new int[ 2 ]; // create 5 columns for row 0 b[ 1 ] = new int[ 2 ]; // create 3 columns for row 1 Or use the initializer list int[][] b = { { 1, 2 }, { 3, 4 } };

Passing arguments to a method Pass-by-value (also called call-by-value) no side effects Pass-by-reference (also called call-by-reference) with side effects All arguments in Java are passed by value. In Java, we can pass a reference by value and produce side effects to the memory referenced to by the reference. (note: reference is a variable who’s value is the address of a memory location) Since array is reference to a memory location (of consecutive elements), when passed to a method, side effects can be made to the memory.

 Variable-length argument lists Definition example: public static double average( double... numbers ) {…} Usage examples: System.out.printf( "Average of d1 and d2 is %.1f\n", average( d1, d2 ) ); System.out.printf( "Average of d1, d2 and d3 is %.1f\n", average( d1, d2, d3 ) );  Command-line arguments Example public static void main( String[] args ) { if ( args.length > 0 ) int i = Integer.parseInt( args[ 0 ] ); …} Usage: java myProgram 8

 import java.util.Arrays; Arrays. sort(), Arrays. binarySearch(), Arrays. equals(), Arrays. fill();  ArrayList add(), remove(), get(), indexOf(), clear(), size(), contains(), …