Document Details

BrainiestOmaha9297

Uploaded by BrainiestOmaha9297

Cavite State University

Jed Quejado

Tags

java array java programming array list programming

Summary

This document provides an introduction to arrays and array lists in Java. It covers topics such as declaring arrays, accessing and modifying array elements, looping through arrays, and using the ArrayList class. The document also includes code examples.

Full Transcript

DCIT 23 - Array Prepared by: Sir Jed Quejado College of Engineering and Information Technology Objective Understand what an array is and how it works. Declare, initialize, and use arrays in Java. Differentiate between arrays and A...

DCIT 23 - Array Prepared by: Sir Jed Quejado College of Engineering and Information Technology Objective Understand what an array is and how it works. Declare, initialize, and use arrays in Java. Differentiate between arrays and ArrayLists. Work with the ArrayList class and its common methods. College of Engineering and Information Technology What is array? An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. Characteristic of array: Store multiple values of the same type. Uses ZERO-based indexing( first elements at index 0) Has a fixed size, meaning it cannot grow or shrink dynamically. Elements are stored in contiguous memory locations. College of Engineering and Information Technology Declaring arrays Declare with values. datatypes identifier[]={“val1”, “val2”, “val3”,…..”valn”}; String name[] = {“juan”, “pedro”, “don marriano”, “Pikachu”}; Declare with no values. datatypes identifier[] = new datatype[size] String name[] = new String; int number[] = new int; College of Engineering and Information Technology Accessing and Modifying Array Elements Use indexing to access elements (arr[index]). Index starts from 0 and goes up to length - 1. Example: int number[] = {10, 20, 30, 40}; System.out.println(number); // output: 20 // modifying an elements number = 35; System.out.println(number) College of Engineering and Information Technology Looping Through an Array Using a tradition for loop. int number[] = {10, 20, 30, 40}; for (int i = 0; i < number.length; i++){ System.out.println(number[i]); //print all element in the array } Using a enhanced for loop(for-each) for (int y : number){ System.out.println(number[y]); //print all element in the array } College of Engineering and Information Technology Looping Through an Array Using a tradition for loop. int number[] = {10, 20, 30, 40}; for (int i = 0; i < number.length; i++){ System.out.println(number[i]); //print all element in the array } Using a enhanced for loop(for-each) for (int y : number){ System.out.println(number[y]); //print all element in the array } College of Engineering and Information Technology What is arraylist? The ArrayList class is a resizable array, which can be found in the java.util package. The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified (if you want to add or remove elements to/from an array, you have to create a new one). While elements can be added and removed from an ArrayList whenever you want. Example Create an ArrayList object called cars that will store strings: import java.util.ArrayList; //import the arraylist class. ArrayList cars = new ArrayList; //create arraylist object College of Engineering and Information Technology Add Items in arraylist The ArrayList class has many useful methods. For example, to add elements to the list, use the add() method: Example College of Engineering and Information Technology Add Items in arraylist You can also add an item at a specified position by referring to the index number: Example College of Engineering and Information Technology ArrayList Access an item - To access an element in the ArrayList, use the get() method and refer to the index number: cars.get(0); Change an Item - To modify an element, use the set() method and refer to the index number: cars.set(0, “kawazaki”) Remove an item – to remove an item, use the remove() method and refer to the index number: cars.remove(0);. To remove all the elements in the ArrayList, use the clear() method: cars.clear(); ArrayList size – to find out how many elements an ArrayList have, use the size method: cars.size(); College of Engineering and Information Technology Looping through an ArrayList Loop through the elements of an ArrayList with a for loop, and use the size() method to specify how many times the loop should run: College of Engineering and Information Technology Looping through an ArrayList You can also loop through an ArrayList with the for-each loop: College of Engineering and Information Technology Other types Elements in an ArrayList are actually objects. In the examples above, we created elements (objects) of type "String". Remember that a String in Java is an object (not a primitive type). To use other types, such as int, you must specify an equivalent wrapper class: Integer. For other primitive types, use: Boolean for boolean, Character for char, Double for double, etc: College of Engineering and Information Technology example Create an ArrayList to store numbers (add elements of type Integer): College of Engineering and Information Technology Thank you  College of Engineering and Information Technology