Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Array Lists and Strings in Java

Uploaded by

saadbink1234
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Array Lists and Strings in Java

Uploaded by

saadbink1234
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

ARRAY LISTS AND

STRINGS IN JAVA
ARRAY LISTS

• Java ArrayList is a part of collections framework and it is a class of


java.util package. It provides us with dynamic arrays in Java.

• Though, it may be slower than standard arrays but can be helpful in


programs where lots of manipulation in array is required.
ARRAY LISTS

• The main advantage of ArrayList is, unlike normal arrays, we don’t


need to mention the size when creating ArrayList. It automatically
adjusts its capacity as elements are added or removed.
SYNTAX OF ARRAYLISTS

ArrayList<Integer> arr = new


ArrayList<Integer>();
Array Lists Example

import java.util.ArrayList;

class Main {
public static void main (String[] args) {

// Creating an ArrayList
ArrayList<Integer> a = new ArrayList<Integer>();

// Adding Element in ArrayList


a.add(1);
a.add(2);
a.add(3);

// Printing ArrayList
System.out.println(a);

}
}
SOME KEY POINTS OF ARRAYLIST IN JAVA

1. ArrayList is Underlined data Structure Resizable Array or Growable Array.

2. ArrayList Duplicates Are Allowed.

3. Insertion Order is Preserved.

4. Heterogeneous objects are allowed.

5. Null insertion is possible.


SOME BUILT IN METHODS OF ARRAY LISTS

• arraylist.clear(); //removes all elements of the arraylist


• arraylist.removeAll(); //removes all elements of the arraylist

• Both of them doing the same task but clear() is faster than removeAll().
SOME BUILT IN METHODS OF ARRAY LISTS

• arraylist.get(1);

//get method takes a single parameter, position of the element and returns the elements
present in the specific position
SOME BUILT IN METHODS OF ARRAY LISTS

• arraylist.indexOf(Object obj)

//The indexOf() method takes a single parameter, obj - element whose position is to be
returned.

//returns the position of the specified element from the arraylist


SOME BUILT IN METHODS OF ARRAY LISTS

• Arraylist.size() //get the number of elements of arraylist


//does not take any argument
//returns the number of elements present in the array list
SOME BUILT IN METHODS OF ARRAY LISTS

arraylist.sort(Comparator.naturalOrder());

// The sort() method takes a single parameter, comparator - specifies the sort order of the
arraylist
// The sort() method does not return any value. Rather it only changes the order of
elements in an arraylist.

 arraylist.sort (Comparator.reverseOrder());
JAVA STRINGS

• In Java, string is basically an object that represents sequence of char


values. An array of characters works same as Java string. For example:
1.char[] ch={‘j','a','v','a','t’,}
2.String s=new String(ch);
• OR
• String s="java";
JAVA STRING CLASS

• Java String class provides a lot of methods to perform operations on


strings such as compare(), concat(), equals(), split(), length(), replace(),
compareTo(), intern(), substring() etc.
JAVA STRING CLASS

• The String class is one of the most fundamental types in Java, designed to represent
immutable sequences of characters. Here's a closer look at its characteristics and the
interfaces it implements:
• Immutability: Once instantiated, a String object cannot be modified. This immutable
design is a deliberate choice to ensure thread safety, consistency, and efficiency, especially
regarding the String pool mechanism.
• String Pool: Java maintains a pool of string literals to help save memory. When a new string
literal is created, Java checks the Pool for a matching string. If found, the new variable
references the pooled string. If not, the new string is added to the Pool.
STRINGBUFFER

• StringBuffer represents a mutable sequence of characters that


ensures thread safety.
• It includes various string manipulation capabilities, including the ability
to insert, delete and append characters.
• This design avoids the necessity of generating new objects with each
change, leading to enhanced efficiency in situations requiring regular
adjustments to the string content.
SYNTAX OF STRINGBUFFER

• StringBuffer sb = new StringBuffer();


STRINGBUILDER

• StringBuilder shares similarities with StringBuffer by being a mutable character


sequence. The crucial distinction lies in StringBuilder not being synchronized, rendering
it not suitable for thread-safe operations.
• This absence of synchronization contributes to StringBuilder offering superior
performance in environments that are single-threaded or confined to a specific thread.
• As a result, StringBuilder becomes the favored option for manipulating strings in contexts
where the safety of concurrent thread access is not an issue.
SYNTAX OF STRINGBUILDER

• StringBuilder sb = new StringBuilder();


IMMUTABLE STRINGS IN JAVA

public class ImmutableString {

public static void main(String[] args) {

String originalString = "Java";

System.out.println("Original string: " + originalString);


String modifiedString = originalString.concat(" Programming");

System.out.println("After modification, original string: " + originalString);

System.out.println("Modified string: " + modifiedString);

originalString.toUpperCase(); // This operation does not change the original string

System.out.println("After calling toUpperCase on original string: " + originalString);

String upperCaseString = originalString.toUpperCase(); // Stores the result in a new string

System.out.println("Original string in uppercase: " + upperCaseString);

} }
SOME BUILTIN METHODS FOR STRINGS

You might also like