Basic Java example program to replace an element at specified index arraylist

1.Basic java Example program to replace an element at specified index java arrayList.

  1. package com.instanceofjavaforus;
  2. import java.util.ArrayList;
  3.  
  4. public class ReplaceArrayList{
  5.  
  6. public static void main(String[] args) {
  7.   
  8. //create an ArrayList object
  9.  ArrayList<Integer> arrayList = new ArrayList<Integer>();
  10.        
  11.         //Add elements to Arraylist
  12.         arrayList.add(1);
  13.         arrayList.add(2);
  14.         arrayList.add(3);
  15.         arrayList.add(4);
  16.         arrayList.add(5);
  17.         arrayList.add(6);
  18.         arrayList.add(7);
  19.         arrayList.add(8);
  20.         arrayList.add(9);
  21.         arrayList.add(10);
  22.         
  23.  
  24.   /*
  25.   To replace an element at the specified index of ArrayList use
  26.   Object set(int index, Object obj) method.
  27.   This Object set(int index, Object obj) method replaces the specified element at the specified
  28.   index in the ArrayList and returns the element previously at the specified position.
  29.   */
  30.  
  31.  arrayList.set(1,23);
  32.      
  33.  System.out.println("ArrayList contains...");
  34.  Iterator itr=arrayList.iterator();
  35.  
  36. while (itr.hasNext()) {
  37.  
  38.  System.out.println(itr.next());
  39.  
  40.  }
  41.   
  42. }
  43. }
     



Output:

  1. ArrayList contains...
  2. 1
  3. 23
  4. 3
  5. 4
  6. 5
  7. 6
  8. 7
  9. 8
  10. 9
  11. 10

Top 10 Java interview questions and programs on this keyword

1.What is this key word in java?

  • "this"is a predefined instance variable to hold current object reference

2.What are the uses of this keyword in constructor?



1.this must be used to access instance variable if both instance and local variable names are same.

  1. package com.instanceofjava;
  2.  
  3. public class ThisDemo {
  4.     int a, b;
  5.  
  6.  ThisDemo(int a, int b){
  7.  
  8.       a=a;
  9.       b=b;
  10.  }
  11.  
  12.     public static void main(String[] args){
  13.  
  14.         ThisDemo obj= new ThisDemo(1,2);
  15.         
  16.         System.out.println(obj.a);
  17.         System.out.println(obj.b);
  18. }
  19. }

Output:

  1. 0
  2. 0


using this keyword:

  1. package com.instanceofjava;
  2.  
  3. public class ThisDemo {
  4.     int a, b;
  5.  
  6.  ThisDemo(int a, int b){
  7.  
  8.       this.a=a;
  9.        this.b=b;
  10.  }
  11.  
  12.     public static void main(String[] args){
  13.  
  14.         ThisDemo obj= new ThisDemo(1,2);
  15.         
  16.         System.out.println(obj.a);
  17.         System.out.println(obj.b);
  18. }
  19. }

Output:

  1. 1
  2. 2


  • We can use this keyword in constructor overloading. 
  • To call one constructor from another we need this(); and this(); call should be first statement of the constructor.

2.Used to invoke current class constructor:

 

  1. package com.instanceofjava;
  2.  
  3. public class ThisDemo {
  4.     int a, b;
  5.  
  6. ThisDemo(){
  7. System.out.println("Default constructor called");
  8. }

  9.  ThisDemo(int a, int b){
  10.        this();
  11.         this.a=a;
  12.        this.b=b;
  13.  }
  14.  
  15.     public static void main(String[] args){
  16.  
  17.         ThisDemo obj= new ThisDemo(1,2);
  18.         
  19.         System.out.println(obj.a);
  20.         System.out.println(obj.b);
  21. }
  22. }

Output:

 

  1. Default constructor called
  2. 1
  3. 2


3. Can we call methods using this keyword?

  • Yes we can use this keyword to call current class non static methods .

  1. package com.instanceofjava;
  2.  
  3. public class Test{
  4.     int a, b;
  5.  

  6.  Test(int a, int b){
  7.       
  8.        this.a=a;
  9.        this.b=b;
  10.  }
  11.  
  12. void show(){
  13.  
  14. System.out.println("Show() method called");
  15.    
  16. }
  17.  
  18. void print(){
  19.  
  20.     this.show();
  21.     System.out.println(a);
  22.     System.out.println(b);
  23.  
  24.  }
  25.     public static void main(String[] args){
  26.  
  27.         Test obj= new Test(1,2);
  28.         
  29.       
  30.         obj.print()
  31. }
  32. }

Output:

 

  1. Show() method called
  2. 1
  3. 2

3. Can we call method on this keyword from constructor?

  • Yes we can call non static methods from constructor using this keyword.

this keyword in java interview questions for freshers




4.Is it possible to assign reference to this ?

  • No we can not assign any value to "this" because its always points to current object and it is a final reference in java.
  • If we try to change or assign value to this compile time error will come.
  • The left-hand side of an assignment must be a variable
this keyword in java with example program
5.Can we return this from a method?

  • Yes We can return this as current class object. 

  1. public class B{

  2.    int a;
  3.     
  4.  public int getA() {
  5.         return a;
  6.  }
  7.  
  8. public void setA(int a) {
  9.         this.a = a;
  10. }
  11.  
  12. B show(){
  13.     return this;
  14. }
  15.  
  16. public static void main(String[] args) {
  17.        
  18.  B obj = new B();
  19.  
  20.   obj.setA(10);
  21.  
  22.  System.out.println(obj.getA());
  23.  B obj2= obj.show();
  24.  System.out.println(obj2.getA());
  25.  
  26. }

  27. }

Output:

  1. 10
  2. 10

6.Can we pass this as parameter of method?

  • Yes we can pass this as parameter in a method

7. Can we use this to refer static members?

  •  Yes its possible to access static variable of a class using this but its discouraged and as per best practices this should be used on non static reference.

8.Is it possible to use this in static blocks?

  •  No its not possible to use this keyword in static block.

use this keyword in static block


9.Can we use this in static methods? 

  • No we can not use this in static methods. if we try to use compile time error will come:Cannot use this in a static context

10.What are all the differences between this and super keyword?

  • This refers to current class object where as super refers to super class object
  • Using this we can access all non static methods and variables. Using super we can access super class variable and methods from sub class.
  • Using this(); call we can call other constructor in same class. Using super we can call super class constructor from sub class constructor.
11. write a java program on constructor overloading or constructor chaining  using this and super keywords

Search an element of Java ArrayList Example

1.Basic java Example program to search an element  ArrayList.

  1. package com.instanceofjavaforus;
  2. import java.util.ArrayList;
  3.  
  4. public class SearchArrayList{
  5.  
  6. public static void main(String[] args) {
  7.   
  8. //create an ArrayList object
  9.  ArrayList<Integer> arrayList = new ArrayList<Integer>();
  10.        
  11.         //Add elements to Arraylist
  12.         arrayList.add(1);
  13.         arrayList.add(2);
  14.         arrayList.add(3);
  15.         arrayList.add(4);
  16.         arrayList.add(5);
  17.         arrayList.add(6);
  18.         arrayList.add(7);
  19.         arrayList.add(8);
  20.         arrayList.add(9);
  21.         arrayList.add(10);
  22.         
  23.  /*
  24.  To check whether the specified element exists in Java ArrayList use
  25. boolean contains(Object element) method.
  26. It returns true if the ArrayList contains the specified object, false
  27. otherwise.*/
  28.        
  29.         boolean isFound = arrayList.contains(2);
  30.         System.out.println("Does arrayList contain 2 ? " + isFound);
  31.      
  32.   /*
  33.    To get an index of specified element in ArrayList use
  34.    int indexOf(Object element) method.
  35.    This method returns the index of the specified element in ArrayList.
  36.    It returns -1 if not found.
  37.   */
  38.    
  39.    int index = arrayList.indexOf(11);
  40.  
  41.  if(index == -1)
  42.     System.out.println("ArrayList does not contain 11");
  43.   else
  44.     System.out.println("ArrayList contains 4 at index :" + index);
  45.         
  46.         
  47.    int secindex = arrayList.indexOf(5);
  48.  
  49.  if(secindex== -1)
  50.       System.out.println("ArrayList does not contain 5");
  51.  else
  52.      System.out.println("ArrayList contains 5 at index :" + secindex);
  53.        
  54.  System.out.println("Size of ArrayList: "+ arrayList.size()); 
  55.  
  56.  Iterator itr=arrayList.iterator();
  57.  
  58. while (itr.hasNext()) {
  59.  
  60.  System.out.println(itr.next());
  61.  
  62.  }
  63.   
  64. }
  65. }
     



Output:

  1. Does arrayList contain 2 ? true
  2. ArrayList does not contain 11
  3. ArrayList contains 5 at index :4
  4. Size of ArrayList: 10
  5. 1
  6. 2
  7. 3
  8. 4
  9. 5
  10. 6
  11. 7
  12. 8
  13. 9
  14. 10

Java Example Program to Remove element from specified index ArrayList

 1.Basic java Example program to remove an element from specified index ArrayList.

  1. package com.instanceofjavaforus;
  2. import java.util.ArrayList;
  3.  
  4. public class RemoveElementArrayList{
  5.  
  6. public static void main(String[] args) {
  7.  
  8.    //create an ArrayList object
  9.         ArrayList<String> arrayList = new ArrayList<String>();
  10.        
  11.         //Add elements to Arraylist
  12.         arrayList.add("A");
  13.         arrayList.add("B");
  14.         arrayList.add("C"); 
  15.         arrayList.add("A");
  16.        
  17.   /*
  18.    To remove an element from the specified index of ArrayList use
  19.     Object remove(int index) method.
  20.     It returns the element that was removed from the ArrayList. */
  21.  
  22.     Object obj = arrayList.remove(1);
  23.      System.out.println(obj + " is removed from ArrayList");
  24.        
  25.   System.out.println("ArrayList contains...");
  26.   //display ArrayList elements using for loop
  27.     for(int index=0; index < arrayList.size(); index++)
  28.     System.out.println(arrayList.get(index));
  29.   
  30.  
  31.  }
  32. }
     


Output:

  1. B is removed from ArrayList
  2. ArrayList contains...
  3. A
  4. C
  5. A

     
 2.Basic java Example program to remove all elements from ArrayList.

  1. package com.instanceofjavaforus;
  2. import java.util.ArrayList;
  3.  
  4. public class RemoveAllElementArrayList{
  5.  
  6. public static void main(String[] args) {
  7.  
  8.    //create an ArrayList object
  9.         ArrayList<String> arrayList = new ArrayList<String>();
  10.        
  11.         //Add elements to Arraylist
  12.         arrayList.add("A");
  13.         arrayList.add("B");
  14.         arrayList.add("C"); 
  15.         arrayList.add("A");
  16.        
  17.  
  18. System.out.println("Size of ArrayList before removing elements : "+ arrayList.size());
  19.  
  20. /*
  21. To remove all elements from the ArrayList we need to use
  22. void clear() method.
  23. */
  24. arrayList.clear();
  25. System.out.println("Size of ArrayList after removing elements : "+ arrayList.size());  
  26.  
  27.  }
  28. }
     
Output:

     
  1. B is removed from ArrayList
  2. ArrayList contains...
  3. A
  4. C
  5. A

Basic Java program to Remove specified element from TreeSet example

1.Basic java example program to remove specified  element from treeset.

  1. package com.TreeSetExamplePrograms;

  2. public Class TreeSetRemoveDemo{ 
  3. import java.util.TreeSet;
  4.  
  5. public static void main(String args[]) {
  6. //create TreeSet object
  7.  
  8. TreeSet<Integer> treeSet = new TreeSet<Integer>();
  9.            
  10.    //add elements to TreeSet object
  11. treeSet.add(11);
  12. treeSet.add(12);
  13. treeSet.add(13);
  14.  
  15. System.out.println("TreeSet before removal : " + treeSet);
  16.            
  17.  /*
  18.  To remove an element from Java TreeSet object use,
  19.  boolean remove(Object o) method.
  20.  This method removes an element from TreeSet if it is present and returns
  21. true. Otherwise remove method returns false.
  22.  */
  23.            
  24.    boolean isRemoved = treeSet.remove(13);
  25.    System.out.println("Was 13 removed from TreeSet ? " + isRemoved);
  26.            
  27.   System.out.println("TreeSet elements after removal : " + treeSet);
  28.           
  29.  
  30. }
  31. }


Output:

  1. TreeSet before removal : [11, 12, 13]
  2. Was 13 removed from TreeSet ? true
  3. TreeSet elements after removal : [11, 12]


Remove all elements from Java TreeSet example:

1.Basic java example program to remove all elements from treeset.


  1. package com.TreeSetExamplePrograms;

  2. public Class TreeSetRemoveDemo{ 
  3. import java.util.TreeSet;
  4.  
  5. public static void main(String args[]) {
  6. //create TreeSet object
  7.  
  8. TreeSet<Integer> treeSet = new TreeSet<Integer>();
  9.            
  10.    //add elements to TreeSet object
  11. treeSet.add(11);
  12. treeSet.add(12);
  13. treeSet.add(13);
  14.  
  15. System.out.println("TreeSet before removal : " + treeSet);
  16.            
  17.  treeSet.clear();
  18. System.out.println("TreeSet after removal : " + treeSet);
  19.            
  20.  /*
  21.  To check whether TreeSet contains any elements or not use
  22.  boolean isEmpty() method.
  23.  isEmpty() method returns true if the TreeSet does not contains any elements
  24.  otherwise returns false.
  25.  */
  26.  
  27.  System.out.println("Is TreeSet empty ? " + treeSet.isEmpty());              
  28.  
  29. }
  30. }




Output:

  1. TreeSet before removal : [11, 12, 13]
  2. TreeSet after removal : []
  3. Is TreeSet empty ? true

Sort employee object in descending order using comparable and TreesSet

1.Basic Java example program sort student object descending order by id using treeset

  1. package com.TreeSetExamplePrograms;

  2. public Class Employee Implements Comparable<Employee>{ 

  3.     String name;
  4.     int id;
  5.     
  6.     public String getName() {
  7.         return name;
  8.     }
  9.  
  10.     public void setName(String name) {
  11.         this.name = name;
  12.     }
  13.  
  14.     public int getId() {
  15.         return id;
  16.     }
  17.  
  18.     public void setId(int id) {
  19.         this.id = id;
  20.     }
  21.  
  22.     Employee (String name, int id){
  23.         this.name=name;
  24.         this.id=id;
  25.     }
  26.  
  27.     @Override    public int compareTo(Employee obj) {
  28.         if (this.getId() == obj.getId()) return 0;
  29.         else
  30.         
  31.         if (this.getId() < obj.getId()) return 1;
  32.         else return -1;
  33.             
  34. }
  35.  
  36. }



  1. package com.TreeSetExamplePrograms;

  2. public Class Test{ 
  3. public static void main(String args[]) {
  4. //create TreeSet object
  5.  
  6. TreeSet<Employee> treeSet = new TreeSet<Employee>();     
  7.      
  8.        //add elements to TreeSet
  9.     treeSet.add(new Employee("abc",1));
  10.     treeSet.add(new Employee("xyz",2));
  11.     treeSet.add(new Employee("ssd",3));
  12.     treeSet.add(new Employee("ert",4));
  13.          
  14.     Iterator itr = treeSet.iterator();
  15.  
  16.  while(itr.hasNext()){
  17.  
  18.      Employee obj= (Employee)itr.next();
  19.      System.out.println(obj.getName()+"  "+obj.getId()); 

  20.   }         

  21. }
  22. }



Output:

  1. ert  4
  2. ssd  3
  3. xyz  2
  4. abc  1 
Select Menu