COMP 110-001 More About Arrays Yi Hong June 05, 2015.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

ArrayLists The lazy mans array. Whats the matter here? int[] list = new int[10]; list[0] = 5; list[2] = hey; list[3] = 15; list[4] = 23;
Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
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 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
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 COMP 110 More Arrays Tabitha Peck M.S. April 2, 2008 MWF 3-3:50 pm Philips 367.
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.
Alice in Action with Java
COMP 110 Introduction to Programming Mr. Joshua Stough October 24, 2007.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Building Java Programs Chapter 7.5
Chapter 9 Introduction to Arrays
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
Multi-Dimensional Arrays in Java "If debugging is the process of removing software bugs, then programming must be the process of putting them in." -- Edsger.
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.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Arrays Arrays are objects that help us organize large amounts of information.
7. Arrays. Topics Declaring and Using Arrays Some Array Algorithms Arrays of Objects Variable Length Parameter Lists Two-Dimensional Arrays The ArrayList.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
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.
AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
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.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Enhanced For Loops (For Each Loops) Less Code and Less Overhead for Writing For Loops.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
C# E1 CSC 298 Arrays in C#. C# E2 1D arrays  A collection of objects of the same type  array of integers of size 10 int[] a = new int[10];  The size.
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.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
The ArrayList Data Structure The Most Important Things to Review.
COMP 110: Spring Announcements Lab 7 was due today Binary Expression Assignment due Friday.
COMP 110 More arrays, 2D arrays, Program 4 Luv Kohli November 10, 2008 MWF 2-2:50 pm Sitterson 014.
COMP 110 Arrays Luv Kohli November 5, 2008 MWF 2-2:50 pm Sitterson 014.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 The if-else Statement An else clause can be added to an if statement to make an if-else statement.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
COMP 110 More arrays, 2D arrays, Program 4
Two-Dimensional Arrays
Michele Weigle - COMP 14 - Spr 04 Catie Welsh March 30, 2011
COMP Final Exam Review Yi Hong June 15, 2015.
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.
Announcements Lab 7 due Wednesday Assignment 4 due Friday.
MSIS 655 Advanced Business Applications Programming
Ps Module 7 – Part II 2D Arrays and LISTS 5/26/2019 CSE 1321 Module 7.
Arrays and ArrayLists.
Arrays.
Why not just use Arrays? Java ArrayLists.
Presentation transcript:

COMP More About Arrays Yi Hong June 05, 2015

Today  More about arrays  2D arrays  Examples of using ArrayList

Arrays as Instance Variables public class Weather { private double[] temperature; private double[] pressure; public void initializeTemperature(int len) { temperature = new double[len]; }

Arrays of Objects  When you create an array of objects like this: Student[] students = new Student[35];  Each of the elements of students is not yet an object  You have to instantiate each individual one students[0] = new Student(); students[1] = new Student();  …or do this in a loop

Arrays of Objects Smiley[] smilies = new Smiley[3]; for (int i = 0; i < smilies.length; i++) { smilies[i] = new Smiley(); } smilies[0].bSmile = true; …… ??? true GREEN 3 false BLUE 1 false CYAN 4

Arrays of Objects Student[] students = new Student[5]; for (int i = 0; i < students.length; i++) { students[i] = new Student(keyboard.nextInt()); students[i].printAge(); }

Arrays as Parameters public void changeArray(int[] arr) { int len = arr.length; arr[len – 1] = 25; }

Arrays as Return Types public double[] buildArray(int len) { double[] retArray = new double[len]; for (int i = 0; i < retArray.length; i++) { retArray[i] = i * 1.5; } return retArray; }

Indexed Variables as Method Arguments  No different from using a regular variable public void printNum(int num) { System.out.println(num); } public void doStuff() { int[] scores = { 15, 37, 95 }; for (int index = 0; index < scores.length; index++) { printNum(scores[index]); }

2D Arrays  Arrays having more than one index are often useful Tables Grids 0: Open1: High2: Low3: Close 0: Apple Inc : Facebook, Inc : Google Inc : Microsoft Corp

Declaring and Creating 2D Arrays int[][] table = new int[4][3]; or int[][] table; table = new int[4][3];

Declaring and Creating 2D Arrays int[][] table = new int[4][3]; gives you the ability to use table[0][0] table[0][1] table[0][2] table[1][0] table[1][1] table[1][2] table[2][0] table[2][1] table[2][2] table[3][0] table[3][1] table[3][2]

How Do You Use a 2D Array?  We used a loop to iterate over a 1D array int[] scores = { 13, 57, 93, 60, 102 }; for (int i = 0; i < scores.length; i++) { System.out.println(scores[i]); }

How do you use a 2D array?  How about a 2D array? int[][] table = new int[4][3];  Use a nested loop for(int row = 0; row < table.length; row++) { for(int column=0; column < table[row].length; column++) { table[row][column] = 30; } }

Length for a 2D Array int[][] table = new int[4][3];  table.length is the number of rows, or the integer in the first pair of brackets (4)  table[i].length is the number of columns, or the integer in the second pair of brackets (3)

Multidimensional Arrays  You can have more than two dimensions int[][][] table = new int[4][3][5];  Use more nested loops to access all elements

Multidimensional Arrays as Parameters public void print2DArray(int[][] arr) { for (int row = 0; row < arr.length; row++) { for (int column = 0; column < arr[row].length; column++) { System.out.print(arr[row][column] + " "); } System.out.println(); }

Multidimensional Arrays as Return Types public int[][] giveMeAnArray() { int[][] table = new int[4][3]; // put values in the table return table; }

Why? Arrays of Arrays int[] scores = new int[5];  scores is a one-dimensional array base type is int int[][] table = new int[4][3];  table is also in fact a one-dimensional array base type is int[]  We still refer to table as a two-dimensional array

2D Array of Irregular Shape int[][] x = new int[3][]; x[0] = new int[6]; x[1] = new int[3]; System.out.println(x[0].length); System.out.println(x[1].length); System.out.println(x[2].length); What’s wrong with the last line?

ArrayList  Array: fixed size once declared 1D, 2D, … n-D 2D array does not have to be rectangle  ArrayList: dynamic size A list that can be manipulated in many ways Very useful in practice In case some students do not read ArrayList documentation, I have several boring slides here.

Creating an ArrayList  In this example we will create an ArrayList of String in Java. This Java ArrayList will only allow String and will throw compilation error if we try to any other object than String. //ArrayList to Store only String objects ArrayList stringList = new ArrayList ();

Putting an Item into ArrayList  Second line will result in compilation error because this Java ArrayList of String will only allow String elements stringList.add("Item"); //no error because we are storing String stringList.add(new Integer(2)); //compilation error

Size & IndexOf  Size of an ArrayList in Java is total number of elements currently stored in ArrayList. int size = stringList.size();  Checking Index of an “Item” in Java Arraylist int index = stringList.indexOf("Item"); //location of Item object in List

Retrieving Item from ArrayList in a Loop for (int i = 0; i < stringList.size(); i++) String item = stringList.get(i); System.out.println(“#" + i + " : " + item); }  Optional: From Java 5 onwards you can use foreach loop as well for(String item: stringList){ System.out.println("retrieved element: " + item); }System

Checking if ArrayList is Empty  Use isEmpty() method of Java ArrayList to check whether ArrayList is empty isEmpty() method returns true if this ArrayList contains no elements. //isEmpty() will return true if List is empty boolean result = stringList.isEmpty();  Or size() method of List to check if List is empty if(stringList.size() == 0){ System.out.println("ArrayList is empty"); }

Removing an Item from ArrayList  Two ways: remove an element based on its index or by providing object itself. remove (Object o) removes the first occurrence of the specified element from this list, if it is present.  Remove first element from ArrayList stringList.remove(0);  Remove first occurrence of item from ArrayList in Java stringList.remove(item);

More About ArrayList  Check Java API

Next Class  Lab 7