Array, String, and Vector – Module 3 Java SBLC

Array, String, and Vector – Module 3 Java SBLC

3.1 Arrays:

  • Array: An array is a collection of elements, all of the same type, stored in contiguous memory locations. The elements can be accessed via indices. Arrays in Java are fixed in size once they are created. Declaring an Array:
  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: A String is a sequence of characters. Strings in Java are immutable, meaning once created, their values cannot be changed. Creating a String:
  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: StringBuffer is used to create mutable (modifiable) strings. It is synchronized, meaning it is thread-safe and can be used in multi-threaded environments. Creating a 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: A Vector is a dynamic array that can grow or shrink in size. It is part of the Java Collection Framework and is synchronized, making it thread-safe. Creating a Vector:
  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));
           }
       }
   }

Team
Team

This account on Doubtly.in is managed by the core team of Doubtly.

Articles: 481