0% found this document useful (0 votes)
91 views

Lesson 1: Java Core: Arrays and Its Variants, String Manipulation

The document discusses Java arrays, strings, and the StringBuilder class. It covers how to create, initialize, access, iterate through, and manipulate arrays and strings in Java. It also provides an overview of the StringBuilder class, including its constructors and common methods to modify the contents dynamically during runtime.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views

Lesson 1: Java Core: Arrays and Its Variants, String Manipulation

The document discusses Java arrays, strings, and the StringBuilder class. It covers how to create, initialize, access, iterate through, and manipulate arrays and strings in Java. It also provides an overview of the StringBuilder class, including its constructors and common methods to modify the contents dynamically during runtime.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Lesson 1: Java Core

Arrays and its Variants, String Manipulation

Arrays
Arrays are container objects that hold a finite number of elements of the same type.
Creating arrays are as follows: int[] array = new int[10];

Initialization and Access


You can also automatically fill an array with contents: int[] x = {1,2,3,4,5,6,7,8};

To access array values just refer to them using their indexes. Java implements 0-indexing. x[7] = 8;

Multi-dimensional Arrays
Multi-dimensional arrays can also be done in Java by simply extending the definition like so: int[][] array = new int [5][10];

arraycopy

Copying arrays can be done by the System class arraycopy method arraycopy(Object array1, int srcStart, Object array2, int destStart, int lengthCopy)

Example of arraycopy
char v = ,b,e,d,o,g-; char c = char[3]; System.arraycopy(v,2,c,0,3); System.out.print(new String(c)); //prints dog

Arrays and its Variants


To access the arrays length, simply access its length property by issuing the following command: int array = new int[10]; System.out.print(array.length);//prints 10

Arrays and its variants


To iterate through an array simply use a for loop or a for-each loop: for(int I = 0; i<array.length; i++){ System.out.print(array[i]); } for(int a: array){ System.out.print(a); }

Strings and String manipulation

LESSON 1: JAVA CORE

Java Strings
Normally Strings are sequences of objects in arrays, in Java Strings are objects. There are multiple ways of creating strings in Java: String s = Harry Styles; String s = new String(*c,a,t+);

Java Strings
To access the length of the String, simply call the length method of the object:
String name = One Direction; int length = name.length(); System.out.print(length)://prints 13

Concatenating Strings
Java provides various ways to concatenate Strings: string1.concat(string2); What makes you.concat( beautiful); Youve got that + one thing!;

Converting Strings to Numbers


Normally data input are String literals that have to be converted to numbers. Wrapper classes have their parsing methods to do this as in: Interger.parseInt(String); Float.parseFloat(String); Byte.parseByte(String)

Converting Values to Strings


All objects in Java (including wrapper classes) have the toString() method and can be used as such:
Integer.toString(1); Double.toString(1);

Converting values to Strings


Or you can just concatenate it with an empty String literal like:
String name = 1 + Direction; String numberOne = 1 + ;

Getting String Characters by Index


Getting a character value on a certain part of a string is as simple as: Zayn Malik.charAt(1); // this is a String values in Java starts with 0 as with arrays and other containers

String substrings
Getting parts of strings or substrings is as simple as: name.substring(int beginIndex, int endIndex); name.substring(int beginIndex); The methods return the string at the beginning index to the end index 1

Other String Methods


String trim() - returns the string without trailing and leading whitespaces toLowerCase() - returns the whole string in lower case toUpperCase() - returns the whole string to upper case

Other String Methods


String split(String regex, int limit) - searches the string for the certain pattern and splits the string into array indexOf(int ch), lastIndexOf(int ch) - returns the last occurrence of the given character contains(CharSequence s) - returns true if the string contains the given character sequence

Replacing Characters and Substrings


replace(char oldChar, char newChar) - returns a new String with all instances of the oldChar replaced with the newChar replaceAll(String regex, String replacement) - replaces all the substrings that match the given regex with the replacement string replaceFirst(String regex, String replacement) - replaces the first substring that matches the regex with the replacement string

Comparing Strings and Portions of Strings


boolean startsWith(String pre), endsWith(String pre) - returns true when the string starts or ends with the given parameter int compareTo(String other) - returns whether the given string is lexicographically greater (>0), equal (=0) or less (<0) (case sensitive)

Comparing Strings
boolean equals(String s) - returns true if the two strings are equal (case sensitive) boolean matches(String regexp) - tests whether the string matches the given regex

String Builder Class Intro


The StringBuilder class is much like the String object except that you can manipulate it during runtime (ie. change length, concatenate within) The StringBuilders also have a length() method.

StringBuilder Class Intro


Unlike the String class, the StringBuilder class has a capacity property (ie number of allocated space for the characters)
You can access this using the capacity() method

StringBuilder Class Intro


Note that the capacity is always greater than or equal to the length that is the capacity is always capable of handling the amount of data in it.

Illustrative Example
capacity

R
length

Creating a StringBuilder Class


There are different constructors for the string builder class:
1. 2. 3. 4. StringBuilder() StringBuilder(CharSequence cs) StringBuilder(int initCapacity) StringBuilder(String s)

Constructors
StringBuilder() - creates an empty StringBuilder class with 16 cells as capacity StringBuilder(CharSequence cs) - creates a StringBuilder with the char sequence plus 16 more cells StringBuilder(int initCapacity) - creates the StringBuilder with the given capacity

Constructor

StringBuilder(String s) - constructs a StringBuilder containing the given string plus 16 more cells for capacity.

Length Operations

void setLength(int newLength) - sets new length of the StringBuilder, truncates when the length is shorter

StringBuilder Methods
append() - appends the given data to the StringBuilder. Converts into a string before appending. delete(int start, int end) - deletes from start to end-1 of the StringBuilder deleteCharAt(int index) - deletes the character at the located index

StringBuilder Methods
replace(int start, int end, String s) - replaces the specified characters in this the StringBuilder reverse() -reverses the sequence of characters in this StringBuilder

You might also like