Matrika je bistvena in najpogosteje uporabljena podatkovna struktura v Java . Je ena najpogosteje uporabljenih podatkovnih struktur programerjev zaradi njene učinkovite in produktivne narave; Array je zbirka podobnih elementov podatkovnega tipa. Za shranjevanje elementov uporablja stalno pomnilniško lokacijo.
'kakšna je razlika med levom in tigrom'
String Array je niz s fiksnim številom nizovnih vrednosti . Niz je zaporedje znakov. Na splošno je niz nespremenljiv predmet, kar pomeni, da vrednosti niza ni mogoče spremeniti. String Array deluje podobno kot druge vrste podatkov Array.
notri Array , je mogoče shraniti samo določen nabor elementov. To je podatkovna struktura, ki temelji na indeksu in se začne z 0thpoložaj. Prvi element bo potekal v indeksu 0, 2ndbo element zasedel mesto v indeksu 1 itd.
Glavna metoda {Public static void main[ String [] args]; } v Javi je tudi String Array.
Razmislite o spodnjih točkah o nizu nizov:
- Je predmet matrike.
- Lahko se deklarira z dvema metodama; z navedbo velikosti ali brez navedbe velikosti.
- Inicializira se lahko bodisi v času deklaracije ali tako, da vnese vrednosti za deklaracijo.
- Elemente je mogoče dodati matriki nizov, potem ko jo deklarirate.
- Niz nizov je mogoče ponavljati z uporabo zanke for.
- Operacijo iskanja in razvrščanja je mogoče izvesti na nizu nizov.
Izjava:
Deklaracija matrike je dveh vrst, lahko podamo velikost matrike ali brez podajanja velikosti matrike. Niz nizov je mogoče deklarirati na naslednji način:
String[] stringArray1 //Declaration of the String Array without specifying the size String[] stringArray2 = new String[2]; //Declarartion by specifying the size
Drug način za razglasitev matrike je Niz strArray[] , vendar so zgoraj navedene metode bolj učinkovite in priporočljive.
Inicializacija:
String Array je mogoče preprosto inicializirati. Spodaj je inicializacija niza nizov:
1. String[] strAr1=new String[] {'Ani', 'Sam', 'Joe'}; //inline initialization 2. String[] strAr2 = {'Ani', 'Sam', ' Joe'}; 3. String[] strAr3= new String[3]; //Initialization after declaration with specific size strAr3[0]= 'Ani'; strAr3[1]= 'Sam'; strAr3[2]= 'Joe';
Vsi zgornji trije načini se uporabljajo za inicializacijo matrike nizov in imajo enako vrednost.
3rdmetoda je metoda določene velikosti. V tem je vrednost indeksa mogoče najti z uporabo ( dolžina niza - 1 ), če želimo dostopati do elementov, ki presegajo indeks 2 v zgornjem polju. Vrglo bo Java.lang.ArrayIndexOutOfBoundsException izjema.
Oglejmo si primer niza nizov, da pokažemo njegovo vedenje:
Ponovitev matrike nizov
Niz nizov je mogoče ponoviti z uporabo zanke for in foreach. Razmislite o spodnji kodi:
String[] strAr = {'Ani', 'Sam', 'Joe'}; for (int i=0; i<strar.length; i++) { system.out.println(strar[i]); } for ( string str: strar) sytem.out.println(str); < pre> <h2>Adding Elements to a String Array</h2> <p>We can easily add the elements to the String Array just like other data types. It can be done using the following three methods:</p> <ul> <tr><td>Using Pre-Allocation of the Array</td> </tr><tr><td>Using the Array List</td> </tr><tr><td>By creating a new Array</td> </tr></ul> <p>let's understand the above methods:</p> <h3>Using Pre-Allocation of the Array:</h3> <p>In this method, we already have an Array of larger size. For example, if we require to store the 10 elements, then we will create an Array of size 20. It is the easiest way to expand the Array elements.</p> <p>Consider the below example to add elements in a pre-allocated array.</p> <pre> // Java Program to add elements in a pre-allocated Array import java.util.Arrays; public class StringArrayDemo { public static void main(String[] args) { String[] sa = new String[7]; // Creating a new Array of Size 7 sa[0] = 'A'; // Adding Array elements sa[1] = 'B'; sa[2] = 'C'; sa[3] = 'D'; sa[4] = 'E'; System.out.println('Original Array Elements:' + Arrays.toString(sa)); int numberOfItems = 5; String newItem = 'F'; // Expanding Array Elements Later String newItem2 ='G'; sa[numberOfItems++] = newItem; sa[numberOfItems++] = newItem2; System.out.println('Array after adding two elements:' + Arrays.toString(sa)); } } </pre> <p> <strong>Output:</strong> </p> <pre> Original Array Elements:[A, B, C, D, E, null, null] Array after adding two elements:[A, B, C, D, E, F, G] </pre> <p>From the above example, we have added two elements in a pre-allocated Array.</p> <h3>Using ArrayList:</h3> <p>The <a href="/java-arraylist">ArrayList</a> is a fascinating data structure of the <a href="/collections-java">Java collection framework</a> . We can easily add elements to a <a href="/java-string">String</a> Array using an ArrayList as an intermediate data structure.</p> <p>Consider the below example to understand how to add elements to a String Array using ArrayList :</p> <pre> import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class StringArrayDemo1 { public static void main(String[] args) { // Defining a String Array String sa[] = { 'A', 'B', 'C', 'D', 'E', 'F' }; // System.out.println('Initial Array: ' + Arrays.toString(sa)); String ne = 'G'; // Define new element to add Listl = new ArrayList( Arrays.asList(sa)); // Convert Array to ArrayList l.add(ne); // Add new element in ArrayList l sa = l.toArray(sa); // Revert Conversion from ArrayList to Array // printing the new Array System.out.println('Array with added Value: ' + Arrays.toString(sa)) ; } } </pre> <p> <strong>Output:</strong> </p> <pre> Initial Array: [A, B, C, D, E, F] Array with added value: [A, B, C, D, E, F, G] </pre> <h3>By Creating a New Array:</h3> <p>In this method, we will create a new Array with a larger size than the initial Array and accommodate the elements in it. We will copy all the elements to the newly added Array.</p> <p>Consider the below example:</p> <pre> // Java Program to add elements in a String Array by creating a new Array import java.util.Arrays; public class StringArrayDemo2 { public static void main(String[] args) { //Declaring Initial Array String[] sa = {'A', 'B', 'C' }; // Printing the Original Array System.out.println('Initial Array: ' + Arrays.toString(sa)); int length_Var = sa.length; //Defining the array length variable String newElement = 'D'; // Defining new element to add //define new array with extended length String[] newArray = new String[ length_Var + 1 ]; //Adding all the elements to initial Array for (int i=0; i <sa.length; i++) { newarray[i]="sa" [i]; } specifying the position of added elements ( last) newarray[newarray.length- 1]="newElement;" make it original and print sa="newArray;" system.out.println('updated array: ' + arrays.tostring(sa)); < pre> <p> <strong>Output:</strong> </p> <pre> Initial Array: [A, B, C] updated Array: [A, B, C, D] </pre> <p>This is how we can add elements to a String Array. Let's understand how to search and sort elements in String Array.</p> <h2>Searching in String Array</h2> <p>For searching a String from the String Array, for loop is used. Consider the below example:</p> <pre> public class StringArrayExample { public static void main(String[] args) { String[] strArray = { 'Ani', 'Sam', 'Joe' }; boolean x = false; //initializing x to false int in = 0; //declaration of index variable String s = 'Sam'; // String to be searched // Iteration of the String Array for (int i = 0; i <strarray.length; i++) { if(s.equals(strarray[i])) in="i;" x="true;" break; } if(x) system.out.println(s +' string is found at index '+in); else not the array'); < pre> <p> <strong>Output:</strong> </p> <pre> Sam String is found at index 1 </pre> <p>In the above example, we have initialized a boolean variable <strong>x</strong> to false and an index variable to iterate through the string. Also, we have declared a local variable String variable <strong>s</strong> to be searched. Here, the break keyword will exit the loop as soon as the string is found.</p> <h2>Sorting in String Array</h2> <p>The sorting in the String array is quite easy. It is performed like in a traditional array. We use a sort() method to sort the Array elements. Sorting is easier than searching.</p> <p>Consider the below example to <a href="/how-sort-string-array-java">sort a String Array</a> :</p> <pre> //Java Program to sort elements in a String Array import java.util.Arrays; public class StringArraySorting { public static void main(String[] args) { // Adding String values String[] colors = {'Cricket','Basketball','Football','Badminton','Tennis'}; // Print Original values System.out.println('Entered Sports: '+Arrays.toString(colors)); Arrays.sort(colors); // Sorting Elements // Print Sorted Values System.out.println('Sorted Sports: '+Arrays.toString(colors)); } } </pre> <p> <strong>Output:</strong> </p> <pre> Entered Sports: [Cricket, Basketball, Football, Badminton, Tennis] Sorted Sports: [Badminton, Basketball, Cricket, Football, Tennis] </pre> <p>From the above example, we can see the elements from a String Array is sorted using the sort() method.</p> <p>We can also convert String Array to other data structures such as List, int Array, ArrayList, and more and vice-versa.</p> <hr></strarray.length;></pre></sa.length;></pre></strar.length;>
Izhod:
Original Array Elements:[A, B, C, D, E, null, null] Array after adding two elements:[A, B, C, D, E, F, G]
Iz zgornjega primera smo dodali dva elementa v predhodno dodeljeno matriko.
Uporaba ArrayList:
The ArrayList je fascinantna podatkovna struktura Ogrodje zbirke Java . Enostavno lahko dodamo elemente v a Vrvica Matrika z uporabo ArrayList kot vmesne podatkovne strukture.
Razmislite o spodnjem primeru, da boste razumeli, kako dodati elemente nizu nizov z uporabo ArrayList:
import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class StringArrayDemo1 { public static void main(String[] args) { // Defining a String Array String sa[] = { 'A', 'B', 'C', 'D', 'E', 'F' }; // System.out.println('Initial Array: ' + Arrays.toString(sa)); String ne = 'G'; // Define new element to add Listl = new ArrayList( Arrays.asList(sa)); // Convert Array to ArrayList l.add(ne); // Add new element in ArrayList l sa = l.toArray(sa); // Revert Conversion from ArrayList to Array // printing the new Array System.out.println('Array with added Value: ' + Arrays.toString(sa)) ; } }
Izhod:
Initial Array: [A, B, C, D, E, F] Array with added value: [A, B, C, D, E, F, G]
Z ustvarjanjem novega polja:
Pri tej metodi bomo ustvarili novo matriko z večjo velikostjo od začetne matrike in vanjo prilagodili elemente. Vse elemente bomo kopirali v novo dodano matriko.
Razmislite o spodnjem primeru:
// Java Program to add elements in a String Array by creating a new Array import java.util.Arrays; public class StringArrayDemo2 { public static void main(String[] args) { //Declaring Initial Array String[] sa = {'A', 'B', 'C' }; // Printing the Original Array System.out.println('Initial Array: ' + Arrays.toString(sa)); int length_Var = sa.length; //Defining the array length variable String newElement = 'D'; // Defining new element to add //define new array with extended length String[] newArray = new String[ length_Var + 1 ]; //Adding all the elements to initial Array for (int i=0; i <sa.length; i++) { newarray[i]="sa" [i]; } specifying the position of added elements ( last) newarray[newarray.length- 1]="newElement;" make it original and print sa="newArray;" system.out.println(\'updated array: \' + arrays.tostring(sa)); < pre> <p> <strong>Output:</strong> </p> <pre> Initial Array: [A, B, C] updated Array: [A, B, C, D] </pre> <p>This is how we can add elements to a String Array. Let's understand how to search and sort elements in String Array.</p> <h2>Searching in String Array</h2> <p>For searching a String from the String Array, for loop is used. Consider the below example:</p> <pre> public class StringArrayExample { public static void main(String[] args) { String[] strArray = { 'Ani', 'Sam', 'Joe' }; boolean x = false; //initializing x to false int in = 0; //declaration of index variable String s = 'Sam'; // String to be searched // Iteration of the String Array for (int i = 0; i <strarray.length; i++) { if(s.equals(strarray[i])) in="i;" x="true;" break; } if(x) system.out.println(s +\' string is found at index \'+in); else not the array\'); < pre> <p> <strong>Output:</strong> </p> <pre> Sam String is found at index 1 </pre> <p>In the above example, we have initialized a boolean variable <strong>x</strong> to false and an index variable to iterate through the string. Also, we have declared a local variable String variable <strong>s</strong> to be searched. Here, the break keyword will exit the loop as soon as the string is found.</p> <h2>Sorting in String Array</h2> <p>The sorting in the String array is quite easy. It is performed like in a traditional array. We use a sort() method to sort the Array elements. Sorting is easier than searching.</p> <p>Consider the below example to <a href="/how-sort-string-array-java">sort a String Array</a> :</p> <pre> //Java Program to sort elements in a String Array import java.util.Arrays; public class StringArraySorting { public static void main(String[] args) { // Adding String values String[] colors = {'Cricket','Basketball','Football','Badminton','Tennis'}; // Print Original values System.out.println('Entered Sports: '+Arrays.toString(colors)); Arrays.sort(colors); // Sorting Elements // Print Sorted Values System.out.println('Sorted Sports: '+Arrays.toString(colors)); } } </pre> <p> <strong>Output:</strong> </p> <pre> Entered Sports: [Cricket, Basketball, Football, Badminton, Tennis] Sorted Sports: [Badminton, Basketball, Cricket, Football, Tennis] </pre> <p>From the above example, we can see the elements from a String Array is sorted using the sort() method.</p> <p>We can also convert String Array to other data structures such as List, int Array, ArrayList, and more and vice-versa.</p> <hr></strarray.length;></pre></sa.length;>
Tako lahko dodamo elemente nizu nizov. Razumejmo, kako iskati in razvrščati elemente v nizu nizov.
Iskanje v nizu nizov
Za iskanje niza iz matrike nizov se uporablja zanka for. Razmislite o spodnjem primeru:
public class StringArrayExample { public static void main(String[] args) { String[] strArray = { 'Ani', 'Sam', 'Joe' }; boolean x = false; //initializing x to false int in = 0; //declaration of index variable String s = 'Sam'; // String to be searched // Iteration of the String Array for (int i = 0; i <strarray.length; i++) { if(s.equals(strarray[i])) in="i;" x="true;" break; } if(x) system.out.println(s +\' string is found at index \'+in); else not the array\'); < pre> <p> <strong>Output:</strong> </p> <pre> Sam String is found at index 1 </pre> <p>In the above example, we have initialized a boolean variable <strong>x</strong> to false and an index variable to iterate through the string. Also, we have declared a local variable String variable <strong>s</strong> to be searched. Here, the break keyword will exit the loop as soon as the string is found.</p> <h2>Sorting in String Array</h2> <p>The sorting in the String array is quite easy. It is performed like in a traditional array. We use a sort() method to sort the Array elements. Sorting is easier than searching.</p> <p>Consider the below example to <a href="/how-sort-string-array-java">sort a String Array</a> :</p> <pre> //Java Program to sort elements in a String Array import java.util.Arrays; public class StringArraySorting { public static void main(String[] args) { // Adding String values String[] colors = {'Cricket','Basketball','Football','Badminton','Tennis'}; // Print Original values System.out.println('Entered Sports: '+Arrays.toString(colors)); Arrays.sort(colors); // Sorting Elements // Print Sorted Values System.out.println('Sorted Sports: '+Arrays.toString(colors)); } } </pre> <p> <strong>Output:</strong> </p> <pre> Entered Sports: [Cricket, Basketball, Football, Badminton, Tennis] Sorted Sports: [Badminton, Basketball, Cricket, Football, Tennis] </pre> <p>From the above example, we can see the elements from a String Array is sorted using the sort() method.</p> <p>We can also convert String Array to other data structures such as List, int Array, ArrayList, and more and vice-versa.</p> <hr></strarray.length;>
V zgornjem primeru smo inicializirali logično spremenljivko x na false in indeksno spremenljivko za ponavljanje skozi niz. Razglasili smo tudi lokalno spremenljivko String variable s da se preišče. Tukaj bo ključna beseda break zapustila zanko takoj, ko bo niz najden.
Razvrščanje v matriki nizov
Razvrščanje v matriki String je zelo enostavno. Izvaja se kot v tradicionalnem nizu. Za razvrščanje elementov Array uporabljamo metodo sort(). Razvrščanje je lažje kot iskanje.
Razmislite o spodnjem primeru razvrstite matriko nizov :
//Java Program to sort elements in a String Array import java.util.Arrays; public class StringArraySorting { public static void main(String[] args) { // Adding String values String[] colors = {'Cricket','Basketball','Football','Badminton','Tennis'}; // Print Original values System.out.println('Entered Sports: '+Arrays.toString(colors)); Arrays.sort(colors); // Sorting Elements // Print Sorted Values System.out.println('Sorted Sports: '+Arrays.toString(colors)); } }
Izhod:
Entered Sports: [Cricket, Basketball, Football, Badminton, Tennis] Sorted Sports: [Badminton, Basketball, Cricket, Football, Tennis]
Iz zgornjega primera lahko vidimo, da so elementi iz matrike nizov razvrščeni z metodo sort().
String Array lahko pretvorimo tudi v druge podatkovne strukture, kot so List, int Array, ArrayList in druge ter obratno.