Professional Documents
Culture Documents
OBJECT ORIENTED
PROGRAMMING
Object Cloning
VASANTHA KUMAR V,
AP/CSE
this keyword in java
Syntax:
import java.util.ArrayList;
ArrayList<Datatype> Array_name= new ArrayList<Datatype>();
Example:
import java.util.ArrayList;
ArrayList<String> cars = new ArrayList<String>();
Example
import java.util.ArrayList;
public class MyClass {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
System.out.println(cars);
}
Access an Item
Example:
cars.get(0);
Change an Item
Example:
cars.set(0, "Opel");
Remove an Item
To remove an element, use the remove() method and refer
to the index number.
Example:
cars.remove(0);
Example:
cars.size();
Loop Through an ArrayList
public class MyClass {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
for (int i = 0; i < cars.size(); i++) {
System.out.println(cars.get(i));
}
Loop Through an ArrayList
public class MyClass {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
for (String i : cars) {
System.out.println(i);
}
Sort an ArrayList
import java.util.ArrayList;
import java.util.Collections; // Import the Collections class
public class MyClass {
public static void main(String[] args) {
ArrayList<String> cars = new ArrayList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.add("Mazda");
Collections.sort(cars); // Sort cars
for (String i : cars) {
Other Types - Example
import java.util.ArrayList;
Example:
String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
System.out.println("The length of the txt string is: " +
txt.length());
UpperCase and LowerCase
Example:
String txt = "Hello World";
System.out.println(txt.toUpperCase());
System.out.println(txt.toLowerCase());
Finding a Character in a String
Example:
String txt = "Please locate where 'locate' occurs!";
System.out.println(txt.indexOf("locate"));
String Concatenation
Example:
String firstName = "John";
String lastName = "Doe";
System.out.println(firstName + " " + lastName);
System.out.println(firstName.concat(lastName));
Special Characters
\\ \ Backslash
Example
String txt = "We are the so-called \"Vikings\" from the north.";
Example 1 :
String x = "10";
String y = "20";
String z = x + y;
Example 2 :
String x = "10";
int y = 20;
String z = x + y;
THANK YOU