Back to the page

In the print window, choose “Save as PDF”.

doubtly doubtly.in

Array, String, and Vector – Module 3 Java SBLC

Java · Updated 26 Jul 2024 · Read online: www.doubtly.in/array-string-and-vector-module-3-java-sblc/

Array, String, and Vector – Module 3 Java SBLC

3.1 Arrays:

  int[] numbers; // Declares an array of integers

Creating and Initializing an Array:

  numbers = new int[5]; // Creates an array of 5 integers
  numbers[0] = 1; // Assigns value to the first element
  numbers[1] = 2; // Assigns value to the second element

Array Initialization with Values:

  int[] numbers = {1, 2, 3, 4, 5}; // Creates and initializes the array

Accessing Array Elements:

  System.out.println(numbers[0]); // Outputs the first element (1)

Iterating through an Array:

  for (int i = 0; i < numbers.length; i++) {
      System.out.println(numbers[i]);
  }

Strings:

  String greeting = "Hello, World!";

Common String Methods:

  String str = "Java Programming";

  int length = str.length(); // Returns the length of the string
  char ch = str.charAt(5); // Returns the character at index 5 ('P')
  String substring = str.substring(5, 16); // Returns a substring ("Programming")
  boolean isEqual = str.equals("java programming"); // Compares strings (false)
  String upper = str.toUpperCase(); // Converts to uppercase ("JAVA PROGRAMMING")

StringBuffer:

  StringBuffer buffer = new StringBuffer("Hello");

Common StringBuffer Methods:

  buffer.append(" World"); // Appends the string to buffer ("Hello World")
  buffer.insert(5, ","); // Inserts a comma at index 5 ("Hello, World")
  buffer.replace(5, 6, ""); // Replaces the comma with an empty string ("Hello World")
  buffer.delete(5, 6); // Deletes the space at index 5 ("HelloWorld")
  String result = buffer.toString(); // Converts StringBuffer to String ("HelloWorld")

Vectors:

  Vector<Integer> vector = new Vector<>();

Common Vector Methods:

  vector.add(1); // Adds an element to the vector
  vector.add(2);
  vector.add(3);

  vector.add(1, 4); // Inserts an element at index 1

  int firstElement = vector.firstElement(); // Retrieves the first element (1)
  int lastElement = vector.lastElement(); // Retrieves the last element (3)

  vector.remove(1); // Removes the element at index 1

  int size = vector.size(); // Returns the size of the vector (3)

  for (int i = 0; i < vector.size(); i++) {
      System.out.println(vector.get(i)); // Iterates through the vector
  }

Examples:

  1. Array Example:
   public class ArrayExample {
       public static void main(String[] args) {
           int[] numbers = {1, 2, 3, 4, 5};
           for (int i = 0; i < numbers.length; i++) {
               System.out.println(numbers[i]);
           }
       }
   }
  1. String Example:
   public class StringExample {
       public static void main(String[] args) {
           String str = "Java Programming";
           System.out.println("Length: " + str.length());
           System.out.println("Character at index 5: " + str.charAt(5));
           System.out.println("Substring: " + str.substring(5, 16));
           System.out.println("Uppercase: " + str.toUpperCase());
       }
   }
  1. StringBuffer Example:
   public class StringBufferExample {
       public static void main(String[] args) {
           StringBuffer buffer = new StringBuffer("Hello");
           buffer.append(" World");
           System.out.println(buffer);
           buffer.insert(5, ",");
           System.out.println(buffer);
           buffer.replace(5, 6, "");
           System.out.println(buffer);
           buffer.delete(5, 6);
           System.out.println(buffer);
       }
   }
  1. Vector Example:
   import java.util.Vector;

   public class VectorExample {
       public static void main(String[] args) {
           Vector<Integer> vector = new Vector<>();
           vector.add(1);
           vector.add(2);
           vector.add(3);
           vector.add(1, 4);
           System.out.println("First element: " + vector.firstElement());
           System.out.println("Last element: " + vector.lastElement());
           vector.remove(1);
           for (int i = 0; i < vector.size(); i++) {
               System.out.println(vector.get(i));
           }
       }
   }

Practise this on Doubtly (free)