String and Array

String and Array in Java

String and Array

Strings

"Strings represent a sequence of characters."

Example

String name = "Hello World"

// String in-built functions

String.length()

This function is used to calculate the length of a string and the output is a int.

Example

  String name = "Hello World";

  int length = name.length();

  System.out.println(length);

Output - 12

String.concat()

This function is used to concatinate 2 strings , second string at the end of first string.

Example

     String start = "Hello";
     String end = "World";
     String concat = start.concat(end);
     System.out.println(concat);

Output

HelloWorld

string.split()

This method is used to convert a string to an array based on regex value.

Example

        String name = "You are a amazing person";
        String[] words = name.split(" ");  // Split based on space

        for(String s : words){
          System.out.println(s);
       }

Output

You are a amazing person

String.toLowerCase()

This method converts all the characters in a string to lower case.

Example

             String name = "Hello";
             System.out.println(name.toLowerCase());

Output

hello

String.toUpperCase()

This method converts all the characters to upper case.

Example

      String name = "Hello";
      System.out.println(name.toUpperCase());

Output

HELLO

String.substring()

This method gives a substring of a string

Example 1

    String name = "Hello World";
    String sub = name.substring(2, 12);
    System.out.println(sub);

Output

llo World

Example 1

    String name = "Hello World";
    String sub = name.substring(3);
    System.out.println(sub);

Output

lo World

# Java Arrays

arrays.png

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

To declare an array, define the variable type with square brackets:

        String[] cars;

We have now declared a variable that holds an array of strings. To insert values to it, we can use an array literal - place the values in a comma-separated list, inside curly braces:

Example1

         String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

Example2

         int[] myNum = {10, 20, 30, 40};

You can also create an array using the new keyword.

      String[] name = new String[size];

Where size is the number of elements to be present Access the Elements of an Array:

You access an array element by referring to the index number.

Example

       String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
       System.out.println(cars[0]);

Outputs

Volvo

Note:

Array indexes start with 0: [0] is the first element. [1] is the second element, etc.

Change an Array Element:

To change the value of a specific element, refer to the index number:

Example

       String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
       cars[0] = "Opel";
       System.out.println(cars[0]);

Now outputs Opel instead of Volvo

Array Length

To find out how many elements an array has, use the length property:

Example

    String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
    System.out.println(cars.length);

Output

4

Loop Through an Array

You can loop through the array elements with the for loop, and use the length property to specify how many times the loop should run. The following example outputs all elements in the cars array:

Example

   String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
    for (int i = 0; i < cars.length; i++) {
       System.out.println(cars[i]);
       }

Output

Volvo BMW Ford Mazda

String-Array-in-Java.jpg

Multidimensional Arrays:

Two Dimensional Arrays

A Two Dimensional array is an array containing one or more 1-D arrays.

To create a two-dimensional array, add each array within its own set of curly braces:

Example:

int[][] twoDimen = {
                {10, 20, 30, 40, 50},

                {20, 30, 40, 50, 60},

                {30, 40, 50, 60, 70}
        };

To access the elements of the twoDimen array, specify two indexes: one for the row and the other for the column.

Example:

    int[][] myNumbers = { {1, 2, 3, 4}, {5, 6, 7} };
    int x = myNumbers[1][2];
    System.out.println(x);

Output

7

Access the elements of 2D array using for loop

Example:

public class TestDrive {
    public static void main(String[] args) {

        int[][] twoDimen = {
                {10, 20, 30, 40, 50},

                {20, 30, 40, 50, 60},

                {30, 40, 50, 60, 70}
        };


         for (int students = 0; students < 3; students++) {
            for (int marks = 0; marks < 5; marks++) {
               System.out.print(twoDimen[students][marks] + " ");
            }
             System.out.println();
         }

    }
}
/*
           Subjects

           sub0    sub1   sub2   sub3   sub4

Student 0   10     20     30     40     50

Student 1   20     30     40     50     60

Student 2   30     40     50     60     70


 */
`

Output

10 20 30 40 50

20 30 40 50 60

30 40 50 60 70

3D arrays

A Three Dimensional array is an array containing one or more 2D arrays.

Example

Suppose if you have represent 3 departments, which will have 2 students each and their respective marks in 3 subjects

        int[][][] threeDimen = {
                {
                        {
                                10, 20, 30
                        },                //Electronics Department
                        {
                                20, 40, 80
                        }
                },
                {
                        {
                                10, 25, 65
                        },                   // CS Department
                        {
                                60, 70, 80
                        }
                },
                {
                        {
                                10, 21, 55
                        },
                        {                         //Mech Department
                                62, 40, 10
                        }
                }