Monday, February 16, 2015

Strings and String Builder

In this Section we are going to see what is Strings and String builders and different kind of operations performed on Strings. Strings are immutable that is once created it cannot be changed and the next change occurs and stored in a different object. However in string builders that is not the case Strings are mutable it can be changed. Please follow the below example for more details


package Example1;

import java.util.Arrays;

public class StringOperations {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  String name="Albert Einstein";
  
  //CharAt
  System.out.println("Character at "+name.charAt(3));
  //uppercase;
  System.out.println("Uppercase "+name.toUpperCase());
  //concat
  System.out.println(name.concat("ka"));
  System.out.println(name);
  
  //equals and equalsignorecase
  if(name.equals("Albert Einstein")){
   System.out.println("its is true");
  }
  
  if(name.equalsIgnoreCase("Albert Einstein")){
   System.out.println("ignores the case ");
  }
  //string contains
  System.out.println("Contains the string " +name.contains("ber"));
  
  //index of
  System.out.println("Index of e is  "+name.indexOf("e"));
  
  //length of the string
  System.out.println("length of String is "+name.length());
  
  //Replace
  System.out.println("Repalce string "+name.replace("A", "Dr A"));
  
  
  //creation of string builder
  StringBuilder names=new StringBuilder("Charles Darwin");
  //append operation
  System.out.println("Uppercase "+names.append("ka"));
  System.out.println(names);
  
  //capacity
  System.out.println(names.capacity());
  
 }

}


output:
Character at e
Uppercase ALBERT EINSTEIN
Albert Einsteinka
Albert Einstein
its is true
ignores the case
Contains the string true
Index of e is 3
length of String is 15
Repalce string Dr Albert Einstein
Uppercase Charles Darwinka
Charles Darwinka
30

>>>Final
------------------------------------------------------------------------------------------------------------

LinkedList

The below Example explains about linked list and the associated methods.The main difference between arraylist and linkedlist is in the usage.
arraylist is better used for searching the element, while linked list is used for inserting deleting.


package Example1;

import java.util.LinkedList;

public class LInkedList {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		LinkedList linkedlistone=new LinkedList();//gerneric linkedlist
		LinkedList names=new LinkedList();//LinkedList specific to String
		
		//adding elements
		names.add("Jill");
		names.add("Jack");
		names.add("Roy");
		
		//Display elements
		
		for(String lists:names){
			System.out.println(lists);
		}
		//get a particular index
		System.out.println("Get the list as the index mentioned "+ names.get(2));
		
		//Get the first value
		System.out.println("Get the first value "+names.getFirst());
		
		//Get the last value
		System.out.println("Get the last value "+names.getLast());
		
		//copy of the linkedlist
		LinkedList newlist=new LinkedList(names);
		System.out.println("New linked list same as names "+newlist.toString());
		
		//check if a element exist
		if(newlist.contains("Jill")){
			System.out.println("Jill is present in the list");
		}
		//Check all as the othe list
		if(newlist.containsAll(names)){
			System.out.println("Both the list matches");
		}
		//index
		System.out.println("Return the index value "+newlist.indexOf("Jack"));
		
		//peek - returns the first element
		System.out.println(newlist.peek());
		
		//Poll - returns teh first element and delete it from the list
		System.out.println(newlist.poll());
		
		for(String display:newlist){
			System.out.println("This is the element in the newlist "+display.toString());
		}
		//push
		newlist.push("Jack");
		newlist.push("derek");
		
		System.out.println(newlist.toString());
		
		//clear
		newlist.clear();
		System.out.println(newlist.toString());
		
	}

}


output:
Jill
Jack
Roy
Get the list as the index mentioned Roy
Get the first value Jill
Get the last value Roy
New linked list same as names [Jill, Jack, Roy]
Jill is present in the list
Both the list matches
Return the index value 1
Jill
Jill
This is the element in the newlist Jack
This is the element in the newlist Roy
[derek, Jack, Jack, Roy]
[]

>>>Final
------------------------------------------------------------------------------------------------------------

ArrayList function

Below are the list of operations that can be performed on the arraylist.

package Example1;

import java.util.ArrayList;
import java.util.Iterator;

public class ArrayList1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		ArrayList newarray=new ArrayList();
		newarray.add("John");
		newarray.add("Kevin");
	
	
//	Iterator ite=newarray.iterator();
//		while(ite.hasNext()){
//			System.out.println(ite.next());
//		}
		
//		for(int i=0;i< newarray.size();i++){
//			System.out.println(newarray.get(i));
//		}
		newarray.remove(1);
		newarray.add(1, "Lisa");
		Boolean containsornot=newarray.contains("Lisa");
		for(String i:newarray){
			System.out.println(i);
		}
		System.out.println(containsornot);
		System.out.println(newarray.indexOf("john"));
		System.out.println(newarray.indexOf("John"));
		System.out.println(newarray.isEmpty());
		System.out.println(newarray.set(1, "kevin"));
		for(String i:newarray){
			System.out.println(i);
		}
		System.out.println(newarray.toString());
	}
}


output: Execute this program to view the result >>>
------------------------------------------------------------------------------------------------------------

ArrayList

ArrayList is a like a array in which you ca add, remove, update the list and its dynamic.Below example explains the arraylist creation, storing values and displaying them.


package Example1;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

public class Arraylist {

 public static void main(String[] args) {
		// TODO Auto-generated method stub
		ArrayList newarry=new ArrayList();
		newarry.add("abc");
		newarry.add("def");
		newarry.add("xyz");
		newarry.add("232");
		newarry.add("121");
		
//		Iterator ite=newarry.iterator();
//		while(ite.hasNext()){
//			System.out.println(ite.next());
//		}
		
//		for(int i=0;i< newarry.size();i++){
//			System.out.println(newarry.get(i));
//		}
		
//		for(String i:newarry){
//			System.out.println(i);
//		}

		//newarry.remove(2);
		//newarry.removeAll(newarry);
		for(String i:newarry){
			System.out.println(i);
		}
	}

}


output:
abc
def
xyz
232
121
iterator is used to display the arraylist or you can use a for loop or enhanced for loop to display the array list. >>>
------------------------------------------------------------------------------------------------------------