2013.03.06. ArrayList Difference of Array and ArrayList [Sample code] TestArrayList.java.

Slides:



Advertisements
Similar presentations
Chapter 10 – Arrays and ArrayLists
Advertisements

Introduction to Programming
Java
Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
The ArrayList Class and the enum Keyword
Two Dimensional Arrays and ArrayList
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;
Java
Chapter 8: Arrays.
Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
Computer Programming Lab(7).
Java POWERED BY: ARVIND DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING RADHA GOVIND GROUP OF INSTITUTIONS,MEERUT.
SCJP 6.0 Lecturer Kuo-Yi Chen 151, 153, 154, 155, 156, 157
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
Computer Programming Lab 8.
Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
CS324e - Elements of Graphics and Visualization A Little Java A Little Python.
Basic Algorithms on Arrays. Learning Objectives Arrays are useful for storing data in a linear structure We learn how to process data stored in an array.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
Java: How to Program Arrays and ArrayLists Summary Yingcai Xiao.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
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.
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.
DREW ALVAREZ AND CORDIE GOODRICH ARRAYS AND ARRAY LISTS.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Programming With Java ICS201 University Of Ha’il1 Chapter 14 Generics and The ArrayList Class.
Chapter 9: Advanced Array Concepts
ArrayList, Multidimensional Arrays
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
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.
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.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
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 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
LinkedList Many slides from Horstmann modified by Dr V.
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
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.
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.
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.
SNU IDB Lab. Ch4. Performance Measurement © copyright 2006 SNU IDB Lab.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Lists, Stacks, Queues, and Priority.
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
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.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
CSE 1201 Object Oriented Programming ArrayList 1.
Lab 3. Why Compressed Row Storage –A sparse matrix has a lot of elements of value zero. –Using a two dimensional array to store a sparse matrix wastes.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
The ArrayList Data Structure The Most Important Things to Review.
Quiz: Design a Product Class Create a definition for a class called Product, which keeps track of the following information: –Name of the product –Weight.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Java Arrays and ArrayLists COMP T1 #5
Introduction to programming in java
Chapter-7 part3 Arrays Two-Dimensional Arrays The ArrayList Class.
The ArrayList Class An ArrayList is a complex data structure that allows you to add or remove objects from a list and it changes size automatically. The.
Chapter 8 Slides from GaddisText
Web Design & Development Lecture 6
Presentation transcript:

ArrayList Difference of Array and ArrayList [Sample code] TestArrayList.java

You can create an arrayto store objects. But, once the array is created, its size is fixed. Java provides the ArrayList class that can be used to store an unlimited number of objects. Methods in ArrayList

/* ArrayList */ class Car { int speed() { return 100; } void run() { System.out.println("Runing!"); } public class TestArrayList public static void main(String[] args) { java.util.ArrayList cityList = new java.util.ArrayList(); cityList.add("Taipei"); cityList.add("Taichung"); cityList.add("Kaushiung"); java.util.ArrayList list = new java.util.ArrayList(); list.add(new Car()); System.out.println(((Car)list.get(0)).speed()); //System.out.println(((Car)list.get(1)).run()); }

ArrayList objects can be used like arrays, but there are many differences. Once an array is created, its size is fixed. You can access an array element using the square-bracket notation (e.g., a[index]). When an ArrayList is created, its size is 0. You cannot use the get and set methods if the element is not in the list. It is easy to add, insert, and remove elements in a list, but it is rather complex to add, insert, and remove elements in an array.

1. (Sort ArrayList)Write the following method that sorts an ArrayList of numbers: Public static void sort(ArrayList list)

2.(Largest rows and columns)Write a program that randomly files in 0s and 1s into an n-by- n matrix, prints the matrix, and finds the rows and columns with the most 1s.(Hint:Use two ArrayLists to store the row and column indices with the most 1s.)Here is a sample run of the program:

Ppt