Download Array strings and exception handling in Java part 5 and more Schemes and Mind Maps Computer science in PDF only on Docsity!
Java Arrays 1. Introduction to Arrays e Definition: A collection of elements of the same data type stored in contiguous memory locations. o Types: One-dimensional, multi-dimensional. 2. Declaring and Initializing Arrays e Declare an array: int[] scores; o Initialize an array: scores = new int [5S]: c Declare and initialize an array in one step: int[] scores = new Int (S]; 3. Accessing Array Elements o Use the index of the element to access it: scores[0] 4. Array Operations o Find the sum of all elements in the array. o Find the maximum element in the array. Java Strings 1. Introduction to Strings o Definition: A sequence of characters. o String class: Java provides a built-in String class to work with strings. 2. String Operations o Concatenation: Use the + operator or concat() method. o Substring: Use the substring() method. o Searching: Use methods like indexOf() or contains(). 3. String Methods o length() : Returns the length of the string. o equals() : Compares two strings for equality. o compareTo() : Compares two strings lexicographically. 5.1 Arrays in Java An array is a collection of elements of the same data type, stored in contiguous memory. 5.1.1 Declaration & Initialization Java int[] arr = new int[5]; // declaration with size int[] nums = {10, 20, 30}; // initialization with values 5.1.2 Types of Arrays 1. One-Dimensional Array Java int[] arr = {1,2,3,4,5}; for(int i=0; i