can you change the head of an arraylist in java code example

Example 1: how to replace an element in array in java

class array{
  public static void main(String[] args){
    int arr[] = {1,2,3,4};
    System.out.println("Before update" + arr[2]);
    arr[2] = 9;//updating the value
    System.out.println("After update" + arr[2]);
  }
}

/*output:-
Before update3
After update9*/

Example 2: java set value of arraylist

import java.util.ArrayList;

public class SetExample {
  public static void main(String args[]) {
    ArrayList<Integer> arraylist = new ArrayList<Integer>();
    arraylist.add(1);
    System.out.println(arrayList.get(0));
    //1
	arrayList.set(0, 2)
    System.out.println(arrayList.get(0));
    //2
  }
}

Tags:

Java Example