An
array is a collection of the same type variable. Whereas a
string is a sequence of Unicode characters or array of characters. Therefore arrays of strings is an array of arrays of characters. Here, string array and arrays of strings both are same term.
For Example, if you want to store the name of students of a class then you can use the arrays of strings. Arrays of strings can be one dimensional or multidimensional.
Declaring the string array: There are two ways to declare the arrays of strings as follows
- Declaration without size:
Syntax:
String[] variable_name;
or
string[] variable_name;
- Declaration with size:
Syntax:
String[] variable_name = new String[provide_size_here];
or
string[] variable_name = new string[provide_size_here];
Example:
// declaration using string keyword
string[] s1;
// declaration using String class object
// by giving its size 4
String[] s2 = new String[4];
Initialization of Arrays of Strings: Arrays can be initialized after the declaration. It is not necessary to declare and initialize at the same time using the new keyword. However, Initializing an Array after the declaration, it must be initialized with the new keyword. It can’t be initialized by only assigning values.
Example:
// Declaration of the array
string[] str1, str2;
// Initialization of array
str1 = new string[5]{ “Element 1”, “Element 2”, “Element 3”, “Element 4”, “Element 5” };
str2 = new string[]{ “Element 1”, “Element 2”, “Element 3”, “Element 4”, “Element 5” };
Note: Initialization without giving size is not valid in C#. It will give compile time error.
Example: Wrong Declaration for initializing an array
// compile-time error: must give size of an array
String[] str = new String[];
// error : wrong initialization of an array
string[] str1;
str1 = {“Element 1”, “Element 2”, “Element 3”, “Element 4” };
Accessing Arrays of Strings Elements: At the time of initialization, we can assign the value. But, we can also assign the value of array using its index randomly after the declaration and initialization. We can access an array value through indexing, placed index of the element within square brackets with the array name.
Example:
// declares & initializes string array
String[] s1 = new String[2];
// assign the value "Geeks" in array on its index 0
s1[0] = 10;
// assign the value "GFG" in array on its index 1
s1[1] = 30;
// assign the value "Noida" in array on its index 2
s1[2] = 20;
// Accessing array elements using index
s1[0]; // returns Geeks
s1[2]; // returns Noida
Declaration and initialization of string array in a single line: String array can also be declared and initialized in a single line. This method is more recommended as it reduces the line of code.
Example:
String[] weekDays = new string[3] {"Sun", "Mon", "Tue", "Wed"};
Code#1: String array declaration, initialization and accessing its elements
CSharp
// C# program to illustrate the String array
// declaration, initialization and accessing
// its elements
using System;
class Geeks {
// Main Method
public static void Main()
{
// Step 1: Array Declaration
string[] stringarr;
// Step 2:Array Initialization
stringarr = new string[3] {"Element 1", "Element 2", "Element 3"};
// Step 3:Accessing Array Elements
Console.WriteLine(stringarr[0]);
Console.WriteLine(stringarr[1]);
Console.WriteLine(stringarr[2]);
}
}
Output:
Element 1
Element 2
Element 3
Code#2: Array declaration and initialization in single line
csharp
// C# code to illustrate Array declaration
// and initialization in single line
using System;
class Geeks {
// Main Method
public static void Main()
{
// array initialization and declaration
String[] stringarr = new String[] {"Geeks", "GFG", "Noida"};
// accessing array elements
Console.WriteLine(stringarr[0]);
Console.WriteLine(stringarr[1]);
Console.WriteLine(stringarr[2]);
}
}
Output:
Geeks
GFG
Noida
Note:
Similar Reads
Bash Scripting - Array
Arrays are important concepts in programming or scripting. Arrays allow us to store and retrieve elements in a list form which can be used for certain tasks. In bash, we also have arrays that help us in creating scripts in the command line for storing data in a list format. In this article, we will
7 min read
Arrays in LISP
LISP, is a list processing, is a programming language widely used in working with data manipulation. LISP allows us to produce single or multiple-dimension arrays using the make-array function. An array can store any LISP object as its elements. Â The index number of the array starts from 0 to the n-
2 min read
Arrays in Objective-C
An Array is a data structure that holds similar types of data in contiguous memory. The Objective-C array is a single variable array that is used to store different types of elements in the array. Also, we can access the objective-c array using an index. It is quite similar to the C programming lang
5 min read
C# Arrays
An array is a group of like-typed variables that are referred to by a common name. And each data item is called an element of the array. The data types of the elements may be any valid data type like char, int, float, etc. and the elements are stored in a contiguous location. Length of the array spe
8 min read
C++ Arrays
In C++, an array is a derived data type that is used to store multiple values of similar data types in a contiguous memory location.Arrays in C++Create an ArrayIn C++, we can create/declare an array by simply specifying the data type first and then the name of the array with its size inside [] squar
10 min read
One Dimensional Arrays in C
In C, an array is a collection of elements of the same type stored in contiguous memory locations. This organization allows efficient access to elements using their index. Arrays can also be of different types depending upon the direction/dimension they can store the elements. It can be 1D, 2D, 3D,
5 min read
Get a Substring in C
A substring is a contiguous sequence of characters within a string. In this article, we will learn how to extract a substring using a C program.The simplest method to get a substring from a larger string is by using strncpy() function. Letâs take a look at an example:C++#include <stdio.h> #inc
2 min read
Array of Pointers in Objective-C
A pointer is a variable that stores the address of another variable. We use pointers because, with the help of pointers the memory is accessed efficiently, it saves memory space, and execution time is faster as compared to the use of normal variables using stack memory because pointers store their m
5 min read
Maximum Size of an Array in C
Array in C is a collection of elements of the same data type that are stored in contiguous memory locations. The size of an array is determined by the number of elements it can store and there is a limit on this size. The maximum size of an array in C is determined by many factors, including the dat
4 min read
Implementation of Stack Using Array in C
A stack is a linear data structure that follows the Last In First Out (LIFO) principle. This means that the most recently added element is the first one to be removed. In this article, we will learn how to implement a stack using an array in C. Implementation of Stack Using Arrays in CIn the array-b
5 min read