1. ArrayList and Iterator in Java
in our example we have defined the ArrayList is of String type. If you are having integer array list then the returned value should be stored in an integer
Raw type
In 2004 Java 5 came out
ArrayLists Generics A data structure is a software construct used to
First we will look at a useful class provided by Java called an ArrayList. For example in our trivia game we declared the array to be of size 5.
2021 AP Exam Administration Student Samples: AP Computer
be unambiguously inferred from context for example
ArrayList
(Java "arrays" which we will study shortly are an alternative to the ArrayList and they can store primitives.) First we will look at a small ArrayList example
CS 106A Lecture 19 ArrayLists
ArrayLists suggested reading: Java Ch. 11.8 Know how to store data in and retrieve data from an ArrayList. ... •ArrayLists. •Example: reversible writing.
Generic ArrayLists Collections
10/03/2016 – Create an ArrayList of generic objects! 5. CSC216: Programming Concepts – Java © NC State CSC216 Faculty. Working with ArrayList. • ...
ArrayList
Example: reading words from a file but you don't ArrayList is an alternative for variable size data ... ArrayList is a class in Java ...
Building Java Programs
in Java a list can be represented as an ArrayList object The type you specify when creating an ArrayList must be.
AP Computer Science A Sample Student Responses and Scoring
unambiguously inferred from context for example
[PDF] 1 ArrayList and Iterator in Java
in our example we have defined the ArrayList is of String type If you are having integer array list then the returned value should be stored in an integerÂ
[PDF] ArrayList - GitHub Pages
Example: reading words from a file but you don't ArrayList is an alternative for variable size data ArrayList is a class in JavaÂ
[PDF] Arrays & ArrayList - Washington
examples found in the Java class libraries: (covered in this course!) — ArrayList LinkedList HashMap TreeSet PriorityQueue
[PDF] ARRAYLIST IN JAVA
ArrayList is a List and implements the java util list interface ArrayList is a better alternative to code ArrayList is used along with generics
[PDF] ArrayLists
Java Collections and ArrayLists ? Java includes a large set of powerful collection classes ? The most basic ArrayList is essentially the
[PDF] Chapitre 12 - Utilisation dobjets : String et ArrayList - Cnam
En java les chaînes de caractères sont des objets Prenons un premier exemple : la méthode length() renvoie la longueur de la chaîne Elle ne
[PDF] Java - The ArrayList Class - Tutorialspoint
The ArrayList class extends AbstractList and implements the List interface ArrayList supports dynamic arrays that can grow as needed Standard Java arrays areÂ
[PDF] 14-ArrayListpdf
The ArrayList class is an example of a collection class • Starting with version 5 0 Java has added a new kind of for loop called a for-each or
[PDF] ArrayList
An ArrayList is an object that can store a group of other objects for us and allow us to manipulate those objects one by one For example we could use anÂ
[PDF] Collections Collections Collections javautilArrayList
exemple: un tableau est une collection ? Classes interfaces ?AbstractCollection ArrayList Arrays Collections HashSet
2ArrayListsHashMapsWearehere
3Learning Goals•KnowhowtostoredatainandretrievedatafromanArrayList.
4Plan for today•Recap:Tic-Tac-Toe•ArrayLists•Example:reversiblewriting•Example:planner•ArrayListsvs.arrays•Recap
5Plan for today•Recap:Tic-Tac-Toe•ArrayLists•Example:reversiblewriting•Example:planner•ArrayListsvs.arrays•Recap
7Plan for today•Recap:Tic-Tac-Toe•ArrayLists•Example:reversiblewriting•Example:planner•ArrayListsvs.arrays•Recap
8Limitations of Arrays•Sizemustbespecifieduponcreation•Can'tadd/remove/insertelementslater•Nobuilt-inmethodsforsearching,etc.•Can'tprintarrayswithoutArrays.toString(orArrays.deepToString)index0123456789value1249-226517-684723
10Our First ArrayListArrayList myArrayList= new ArrayList<>();
11Our First ArrayListArrayList myArrayList= new ArrayList<>();import java.util.*;
12Our First ArrayListArrayList myArrayList= new ArrayList<>();
13Our First ArrayListArrayList myArrayList= new ArrayList<>();
14Our First ArrayListArrayList myArrayList= new ArrayList<>();Type of items your ArrayListwill store.
15Our First ArrayListArrayList myArrayList= new ArrayList<>();
16Our First ArrayListArrayList myArrayList= new ArrayList<>();
17Our First ArrayListArrayList myArrayList= new ArrayList<>();
18Our First ArrayList// Create an (initially empty) listArrayList list = newArrayList<>();
19Our First ArrayList// Create an (initially empty) listArrayList list = newArrayList<>();// Add an element to the backlist.add("Hello");// now size 1"Hello"
20Our First ArrayList// Create an (initially empty) listArrayList list = newArrayList<>();// Add an element to the backlist.add("Hello");// now size 1list.add("there!");// now size 2"Hello""Hello""there!"
21Our First ArrayList// Add an element to the backlist.add("Hello");// now size 1list.add("there!");// now size 2// Access elements by index (starting at 0!)println(list.get(0));// prints "Hello"println(list.get(1));// prints "there!"println(list);// prints ["Hello", "there!"]"Hello""Hello""there!"
22Our First ArrayList// Add an element to the backlist.add("Hello");// now size 1list.add("there!");// now size 2// Access elements by index (starting at 0!)for(inti= 0; i< list.size(); i++) {println(list.get(i));}"Hello""Hello""there!"
23Our First ArrayList// Add an element to the backlist.add("Hello");// now size 1list.add("there!");// now size 2// Access elements by index (starting at 0!)for(inti= 0; i< list.size(); i++) {println(list.get(i));}"Hello""Hello""there!"
24Our First ArrayList// Add an element to the backlist.add("Hello");// now size 1list.add("there!");// now size 2// Access elements in order (also for arrays!)for(String str: list) {println(str);}"Hello""Hello""there!"
25Iterating Over ArrayLists// Access elements in order (also for arrays!)for(String str: list) {println(str);}// equivalent tofor(inti= 0; i< list.size(); i++) {String str= list.get(i);println(str);}
26Iterating Over ArrayLists// Access elements in order (also for arrays!)for(String str: list) {println(str);}// equivalent tofor(inti= 0; i< list.size(); i++) {String str= list.get(i);println(str);}
27Bad Times with ArrayLists// Create an (initially empty) listArrayList list = newArrayList<>();// Wrong type -bad times! Won't compileGLabellabel = newGLabel("Hello there!");list.add(label);// Invalid index! IndexOutOfBoundsExceptionprintln(list.get(2));
28Plan for today•Recap:Tic-Tac-Toe•ArrayLists•Example:reversiblewriting•Example:planner•ArrayListsvs.arrays•Recap
29Example: Reversible WritingIamnotapersonwhocontributesAndIrefusetobelievethatIwillbeusefulLet's write a program that reverses a text file.
30Example: Reversible WritingIamnotapersonwhocontributesAndIrefusetobelievethatIwillbeusefulIwillbeusefulAndIrefusetobelievethatIamnotapersonwhocontributesLet's write a program that reverses a text file."I Have a Dream" by AntoniaLee, SaraFung, ChristyFung, RachelLamhttp://poets.spice.org.hk/index.php?option=com_content&view=article&id=45:my-family&catid=6:reverse-poem&Itemid=7
31Example: Reversible WritingLet's write a program that reverses a text file."Iamnotapersonwhocontributes"
32Example: Reversible WritingLet's write a program that reverses a text file."Iamnotapersonwhocontributes""AndIrefusetobelievethat"
33Example: Reversible WritingLet's write a program that reverses a text file."Iamnotapersonwhocontributes""AndIrefusetobelievethat""Iwillbeuseful"Key idea: fill an ArrayListwith each line in the file
34Example: Reversible WritingLet's write a program that reverses a text file."Iamnotapersonwhocontributes""AndIrefusetobelievethat""Iwillbeuseful"
35Example: Reversible WritingLet's write a program that reverses a text file."Iamnotapersonwhocontributes""AndIrefusetobelievethat""Iwillbeuseful"Key idea: print the ArrayListitems in reverse order
36Example: Reversible WritingString filename = promptUserForFile("Filename: ", "res");try{Scanner s = newScanner(newFile(filename));ArrayList lines = newArrayList<>();// Read all lines and store in our ArrayListwhile(scanner.hasNextLine()) {lines.add(scanner.nextLine());}// Output the lines from back to frontfor(inti= lines.size() -1; i>= 0; i--) {println(lines.get(i));}} catch(IOExceptionex) {println("Could not read file.");}
37Example: Reversible WritingString filename = promptUserForFile("Filename: ", "res");try{Scanner s = newScanner(newFile(filename));ArrayList lines = newArrayList<>();// Read all lines and store in our ArrayListwhile(scanner.hasNextLine()) {lines.add(scanner.nextLine());}// Output the lines from back to frontfor(inti= lines.size() -1; i>= 0; i--) {println(lines.get(i));}} catch(IOExceptionex) {println("Could not read file.");}
38Example: Reversible WritingString filename = promptUserForFile("Filename: ", "res");try{Scanner s = newScanner(newFile(filename));ArrayList lines = newArrayList<>();// Read all lines and store in our ArrayListwhile(scanner.hasNextLine()) {lines.add(scanner.nextLine());}// Output the lines from back to frontfor(inti= lines.size() -1; i>= 0; i--) {println(lines.get(i));}} catch(IOExceptionex) {println("Could not read file.");}
39Example: Reversible WritingString filename = promptUserForFile("Filename: ", "res");try{Scanner s = newScanner(newFile(filename));ArrayList lines = newArrayList<>();// Read all lines and store in our ArrayListwhile(scanner.hasNextLine()) {lines.add(scanner.nextLine());}// Output the lines from back to frontfor(inti= lines.size() -1; i>= 0; i--) {println(lines.get(i));}} catch(IOExceptionex) {println("Could not read file.");}
40Example: Reversible WritingString filename = promptUserForFile("Filename: ", "res");try{Scanner s = newScanner(newFile(filename));ArrayList lines = newArrayList<>();// Read all lines and store in our ArrayListwhile(scanner.hasNextLine()) {lines.add(scanner.nextLine());}// Output the lines from back to frontfor(inti= lines.size() -1; i>= 0; i--) {println(lines.get(i));}} catch(IOExceptionex) {println("Could not read file.");}
41Example: Reversible WritingString filename = promptUserForFile("Filename: ", "res");try{Scanner s = newScanner(newFile(filename));ArrayList lines = newArrayList<>();// Read all lines and store in our ArrayListwhile(scanner.hasNextLine()) {lines.add(scanner.nextLine());}// Output the lines from back to frontfor(inti= lines.size() -1; i>= 0; i--) {println(lines.get(i));}} catch(IOExceptionex) {println("Could not read file.");}
42Example: Reversible WritingString filename = promptUserForFile("Filename: ", "res");try{Scanner s = newScanner(newFile(filename));ArrayList lines = newArrayList<>();// Read all lines and store in our ArrayListwhile(scanner.hasNextLine()) {lines.add(scanner.nextLine());}// Output the lines from back to frontfor(inti= lines.size() -1; i>= 0; i--) {println(lines.get(i));}} catch(IOExceptionex) {println("Could not read file.");}
43Example: Reversible WritingString filename = promptUserForFile("Filename: ", "res");try{Scanner s = newScanner(newFile(filename));ArrayList lines = newArrayList<>();// Read all lines and store in our ArrayListwhile(scanner.hasNextLine()) {lines.add(scanner.nextLine());}// Output the lines from back to frontfor(inti= lines.size() -1; i>= 0; i--) {println(lines.get(i));}} catch(IOExceptionex) {println("Could not read file.");}
44Plan for today•Recap:Tic-Tac-Toe•ArrayLists•Example:reversiblewriting•Example:planner•ArrayListsvs.arrays•Recap
45ArrayListMethodslist.add(value);appendsvalueatendoflistlist.add(index, value);insertsgivenvaluejustbeforethegivenindex,shiftingsubsequentvaluestotherightlist.clear();removesallelementsofthelistlist.get(index)returnsthevalueatgivenindexlist.indexOf(value)returnsfirstindexwheregivenvalueisfoundinlist(-1ifnotfound)list.isEmpty()returnstrueifthelistcontainsnoelementslist.remove(index);removes/returnsvalueatgivenindex,shiftingsubsequentvaluestotheleftlist.remove(value);removesthefirstoccurrenceofthevalue,ifanylist.set(index, value);replacesvalueatgivenindexwithgivenvaluelist.size()returnsthenumberofelementsinthelistlist.toString()returnsastringrepresentationofthelistsuchas"[3, 42, -7, 15]"
46Insert/remove•Ifyouinsert/removeinthefrontormiddleofalist,elementsshifttofit.list.add(2, 42);•shiftelementsrighttomakeroomforthenewelementlist.remove(1);•shiftelementslefttocoverthespaceleftbytheremovedelementindex01234value38975index012345value3842975index012345value3842975index01234value342975
47Example: Planner
48Example: Planner•Let'swriteaprogramtohelpplanoutourday-Theprogramfirstpromptsforthingsyouwanttodotoday-Then,itaskstheusertore-inputtheminorderofcompletion-Finally,itoutputstheordertheuserhaschosenfortheirtasks
49Planner: Approach"Walk Daisy"Todos:
50Planner: Approach"Walk Daisy""Play Zelda"Todos:
51Planner: Approach"Walk Daisy""Play Zelda""Lunch with Avi"Todos:
52Planner: Approach"Walk Daisy""Play Zelda""Lunch with Avi"Todos:Order:"WalkDaisy"
53Planner: Approach"Play Zelda""Lunch with Avi"Todos:Order:"WalkDaisy"
54Planner: Approach"Play Zelda"Todos:Order:"WalkDaisy""Lunch with Avi"
55Planner: ApproachTodos:Order:"WalkDaisy""Lunch with Avi""Play Zelda"DONE!
56Plan for today•Recap:Tic-Tac-Toe•ArrayLists•Example:reversiblewriting•Example:planner•ArrayListsvs.arrays•Recap
57ArrayLists+ Primitives = í ½
// Doesn't compile LArrayList
58ArrayLists+ Primitives = í ½
// Use wrapper classes when making an ArrayListArrayList
60Array vs. ArrayListArrayListArrayList list = new ArrayList<>();list.add(1); // [1]list.add(2); // [1, 2]list.set(0, 3); // [3, 2]intx = list.get(0); // 3list.add(4); // [3, 2, 4]list.contains(2); // trueArrayint[] arr= new int[2]; // [0, 0]arr[0] = 1; // [1, 0]arr[1] = 2; // [1, 2]arr[0] = 3; // [3, 2]intx = arr[0]; // 3[no equivalent]
61Array vs. ArrayListWhydobothoftheseexistinthelanguage?-ArraysareJava'sfundamentaldatastorage-ArrayListisalibrarybuiltontopofanarrayWhenwouldyouchooseanarrayoveranArrayList?-Whenyouneedafixedsizethatyouknowaheadoftime-Simplersyntaxforgetting/setting-Moreefficient-Multi-dimensionalarrays(e.g.images)-Histograms/tallying
62Plan for today•Recap:Tic-Tac-Toe•ArrayLists•Example:reversiblewriting•Example:planner•ArrayListsvs.arrays•Recap
quotesdbs_dbs26.pdfusesText_32[PDF] exemple arraylist java
[PDF] créer une arraylist java
[PDF] constructeur arraylist java
[PDF] arraylist<int>
[PDF] droit d'arrestation article
[PDF] interpellation police a domicile
[PDF] arrestation enquête préliminaire
[PDF] arrestation procédure pénale
[PDF] heure légale arrestation
[PDF] enquete preliminaire et garde a vue
[PDF] est ce que la police a le droit de rentrer chez moi
[PDF] arrestation citoyenne france
[PDF] article interpellation preliminaire
[PDF] droit lors d une arrestation