Answers for "ArrayList set(int index E element) method in java"

6

ArrayList E get(int index) method in java

import java.util.ArrayList;
public class ArrayListGetExample
{
   public static void main(String[] args)
   {
      ArrayList<String> al = new ArrayList<String>();
      al.add("lion");
      al.add("tiger");
      al.add("deer");
      al.add("cheetah");
      al.add("elephant");
      al.add("fox");
      al.add("hyenas");
      al.add("jackal");
      System.out.println("Get second element of ArrayList: " + al.get(1));
      System.out.println("Get third element of ArrayList: " + al.get(2));
      System.out.println("Get fifth element of ArrayList: " + al.get(4));
      System.out.println("Get sixth element of ArrayList: " + al.get(5));
   }
}
Posted by: Guest on October-30-2020
1

ArrayList set(int index E element) method in java

example on ArrayList set(int index, E element) method for IndexOutOfBoundsException
import java.util.ArrayList;
public class ArrayListSetMethodExample
{
   public static void main(String[] args)
   {
      try
      {
         ArrayList<Integer> al = new ArrayList<Integer>();
         al.add(36);
         al.add(23);
         al.add(39);
         al.add(69);
         al.add(56);
         System.out.println("ArrayList before using set() method: " + al);
         // replace number at the index 7 with 25
         System.out.println("Trying to replace the element at index greater than capacity: ");
         int num = al.set(7, 25);
         // printing modified ArrayList
         System.out.println("ArrayList after using set() method: " + al);
         // printing replaced element
         System.out.println("Replaced number: " + num);
      }
      catch(IndexOutOfBoundsException ex)
      {
         System.out.println("Exception: " + ex);
      }
   }
}
Posted by: Guest on October-28-2020
2

ArrayList set(int index E element) method in java

import java.util.ArrayList;
public class ArrayListSetMethodExample
{
   public static void main(String[] args)
   {
      ArrayList<String> names = new ArrayList<String>(5);
      names.add("vinay");
      names.add("ajay");
      names.add("vijay");
      names.add("bharat");
      names.add("dinesh");
      System.out.println("ArrayList before using set() method: " + names);
      // change vijay name to chandan
      System.out.println("ArrayList after using set() method: ");
      names.set(2, new String("chandan"));
      for(int a = 0; a < 5; a++)
      {
         System.out.println(names.get(a).toString());
      }
   }
}
Posted by: Guest on October-28-2020

Code answers related to "ArrayList set(int index E element) method in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language