Java. Arrays of strings. One-dimensional and two-dimensional arrays of strings. Initialization of arrays of strings. Examples of tasks solving




Arrays of strings in Java. One-dimensional and two-dimensional arrays of strings. Initialization of arrays of strings. Examples of tasks solving


Contents


Search other websites:

1. The concept of an array of strings. The general form of declaring a one-dimensional array of strings

Like any programming language, the Java programming language can implement arrays of strings. Any string in Java is of type String. The one-dimensional array of strings is of type String[]. The two-dimensional array of strings is of type String[][].

The general form of the declaration and the memory allocation for a one-dimensional array of strings is as follows

String[] arrayName = new String[size];

here

  • String – a built-in Java class that implements a string of characters. An object of type String that supports a large set of operations that can be viewed here and here;
  • arrayName is the name of an object (instance) of type String. In fact, arrayName is a reference to an object of type String;
  • size – array size (number of strings, number of elements of type String).

Declaring a one-dimensional array of strings and allocating memory for it can be implemented in another way

String[] arrayName;
arrayName = new String[size];

 

2. How is a one-dimensional array of strings declared? Example

Below is an example of declaring and using a one-dimensional array of strings.

// declaring a one-dimensional array of strings
String[] array = new String[5];

// fill with initial values
array[0] = "abcd";
array[1] = "Hello";
array[2] = ""; // empty string
array[3] = "bestprog";
array[4] = ";:\\+="; // the combination "\\" is replaced by "\"

// use in expressions
arrayS[4] = arrayS[1] + " " + arrayS[3]; // arrayS[4] = "Hello bestprog"
arrayS[4] += ".net"; // arrayS[4] = "Hello bestprog.net"

As you can see from the example, working with arrays of strings in Java is quite convenient and does not require complex additional transformations to process them.

 

3. Two-dimensional array of strings. General form

Perhaps in some tasks there will be a need to declare a two-dimensional array of strings.

The general form for declaring a two-dimensional array of strings is as follows:

String[][] matrName = new String[n][m];

here

  • matrName – the name of the object (object reference), which is a two-dimensional array of type String;
  • n – number of rows in the matrName array;
  • m – number of columns in the matrName array.

Another way to declare and allocate memory for a two-dimensional array of strings is also possible:

String[][] matrName; // reference declaration to a two-dimensional array of strings
matrName = new String[n][m];

 

4. An example of declaring and using a two-dimensional array of strings

Below is an example of declaring and using a two-dimensional array of strings.

// declaration of a two-dimensional array of strings
String[][] matr = new String[2][3];
// filling the array with values
for (int i=0; i<matrS.length; i++)
    for (int j=0; j<matrS[i].length; j++)
        matrS[i][j] = "matrS[" + i + "][" + j + "]";
// checking
String s;
s = matrS[0][0]; // s = "matrS[0][0]"
s = matrS[1][1]; // s = "matrS[1][1]"


 

5. How is the length of an array of strings determined? The length property. Example

To determine the number of lines in an array, use the length property. For one-dimensional arrays, the number of rows n is determined as follows:

String[] arrayS = new String[25];
int n;
n = array.length;

For two-dimensional arrays, the number of rows and columns is determined as follows.

// matr - two-dimensional array of strings
String[][] matrS = new String[2][3];

int n, m;
n = matr.length; // n = 2 - number of rows
m = matr[0].length; // m = 3 - number of columns
m = matr[1].length; // m = 3

 

6. How is initialization of a one-dimensional array? Example

Initialization of a one-dimensional array of strings is exactly the same as initialization of a one-dimensional array of any other type.

// initialization of a one-dimensional array of strings
String[] M = {
    "Sunday",
    "Monday",
    "Tuesday",
    "Wednesday",
    "Thursday",
    "Friday",
    "Saturday"
};
String s;

s = M[2]; // s = "Tuesday"
s = M[4]; // s = "Thursday"

 

7. Search for a given string in a one-dimensional array of strings. Example
// search for a given string in an array of strings
// string array declaration
String M[] = new String[5];
String s = "May"; // string to find
boolean f_is;

// filling the array with values
M[0] = "January";
M[1] = "February";
M[2] = "May";
M[3] = "October";
M[4] = "December";

// string search
f_is = false;
for (int i=0; i<M.length; i++)
    if (M[i]==s) {
        f_is = true;
        break;
}

// output of the result
if (f_is)
    System.out.println("The search string is in the array.");
else
    System.out.println("The string is not in the array.");

 

8. Sorting a one-dimensional array of strings alphabetically by the insertion. Example

To compare two strings in lexicographical order, the String class has developed the compareTo() method. The general form of the method is as follows:

int compareTo(second_string)

Method returns

  • <0, if the second string follows the first string in lexicographical order;
  • =0, if the strings are identical;
  • >0, if the second string follows the first in lexicographical order.

Fragment that demonstrates the sorting of a string array using insertion sort algorithm:

// string sorting using insertion sort
String[] M = {
    "abc",
    "bde",
    "fgh",
    "abcd",
    "bcdef",
    "cdef",
    "fghij",
    "aaa"
};
String s;

// sorting
for (int i=0; i<M.length-1; i++)
    for (int j=i; j>=0; j--)
        if (M[j].compareTo(M[j+1])>0) {
            // exchange M[j] and M[j+1]
            s = M[j];
            M[j] = M[j+1];
            M[j+1] = s;
        }

// output of the result
for (int i=0; i<M.length; i++)
    System.out.println(M[i]);

As a result of executing the above code, the following will be displayed

aaa
abc
abcd
bcdef
bde
cdef
fgh
fghij

 

9. How is the initialization of a two-dimensional array of strings? Example

Initialize two-dimensional array of rows is the same as the two-dimensional array of any initialization primitive type. The array items are normal strings. Below is an example of initializing a two-dimensional array of strings named M

// declaration an array M with initialization
String M[][] = new {
    { "a1", "a2", "a3" },
    { "b1", "b2", "b3" },
    { "a1", "c2", "a1" }
};

// checking
String s;
s = M[0][1]; // s = "a2"
s = M[1][0]; // s = "b1"





 

10. An example of counting the number of occurrences of a given string in a two-dimensional array of strings
// calculating the number of occurrences of a given string in a two-dimensional array
// declaration of array M with initialization
String M[][] = {
    { "abcd", "abc", "bcd" },
    { "acd", "bcd", "abcd" },
    { "abc", "bc", "cde" }
};

String s = "abc"; // line, the number of occurrences of which is necessary to calculate
int k = 0; // number of occurrences, the result
for (int i=0; i<M.length; i++)
    for (int j=0; j<M[i].length; j++)
        if (M[i][j]==s)
            k++;
// k = 2

 

11. Example of replacing a string in a two-dimensional array of strings

Given:

  • a two-dimensional array of strings named matr;
  • string s1, which is searched for replacement;
  • the string s2, which replaces the string s1.

Develop a program that replaces the string s1 in the matrix matr with the new string s2. The code snippet that solves this task:

// declaration of a two-dimensional array of strings
String[][] matr = new String[2][3];

// filling matr matrix with arbitrary values
matrS[0][0] = "abc";
matrS[0][1] = "cba";
matrS[0][2] = "def";
matrS[1][0] = "abc";
matrS[1][1] = "fff";
matrS[1][2] = "qqq";

// filling in the values of strings s1 and s2
String s1 = "abc"; // the string to be replaced
String s2 = "mmm"; // the string that replaces

// calculation loop
for (int i=0; i<matrS.length; i++)
    for (int j=0; j<matrS[i].length; j++)
        if (matrS[i][j]==s1)
            matr[i][j] = s2;

// result output
for (int i=0; i<matrS.length; i++) {
    for (int j=0; j<matrS[i].length; j++)
        System.out.print(matrS[i][j] + " ");
    System.out.println();
}

As a result of executing the above code, the following result will be displayed:

mmm cba def
mmm fff qqq

 


Related topics