logo

Array Slicing Javassa

Javassa array viipalointi on tapa saada annetusta taulukosta alitaulukko. Oletetaan, että a[] on taulukko. Siinä on 8 elementtiä indeksoituna a[0]:sta a[7]:ään.

a[] = {8, 9, 4, 6, 0, 11, 45, 21}

vertailukelpoinen java

Nyt haluamme löytää osan taulukkoindeksistä a[3]:sta a[6]:aan. Missä a[3] on aloitusindeksi ja a[6] on endIndex. Siksi saamme seuraavan viipaloitu joukko :

a[] = {6, 0, 11, 45}

Tässä osiossa opimme kuinka löytää osa taulukosta Javassa.

On olemassa kolme tapaa löytää taulukon osio:

  • Kopioimalla elementtejä
  • Käyttämällä copyOfRange()-menetelmää
  • Java 8 Streamin käyttö

Keskustelemme jokaisesta menetelmästä yksityiskohtaisesti.

lista solmu javassa

Kopioimalla elementtejä

Se on natiivi tapa saada siivu taulukosta. Tässä menetelmässä etsitään ensin annetun taulukon alku- ja loppuindeksi. Tämän jälkeen luomme tyhjän taulukon (viipaloidun taulukon). (endIndex - startIndex). Kopioi annetusta taulukosta elementit (startIndexistä) viipaloituun taulukkoon. Tulosta lopuksi viipaloitu taulukko.

Toteutetaan yllä oleva lähestymistapa a Java ohjelma saada viipaloitu taulukko annetusta taulukosta. Tässä ohjelmassa. käytämme joukkoa primitiivisiä tyyppejä.

SliceArrayExample1.java

 import java.util.Arrays; public class SliceArrayExample1 { //creating a functiion to the slice of an array public static int[] getSlice(int[] array, int startIndex, int endIndex) { // Get the slice of the Array int[] slicedArray = new int[endIndex - startIndex]; //copying array elements from the original array to the newly created sliced array for (int i = 0; i <slicedarray.length; i++) { slicedarray[i]="array[startIndex" + i]; } returns the slice of an array return slicedarray; main() method public static void main(string args[]) from which we will find int[] 56, 78, 22, 45, 90, 67, 91, 0, 31}; start index and end denotes part original to be int startindex="3," endindex="8;" get slicedarray="getSlice(array," startindex, 1); prints system.out.println('slice array: '+arrays.tostring(slicedarray)); < pre> <p> <strong>Output:</strong> </p> <pre> Slice of Array: [22, 45, 90, 67, 91, 0] </pre> <h2>By Using the copyOfRange() Method</h2> <p>The copyOfRange() method belongs to the Java Arrays class . It copies the specified range of the array to the newly created array (slice array) and returns the newly created array that contains the specified range from the original array. It takes <strong>O(n)</strong> time to create slicing of an array and <strong>O(n)</strong> space to store elements, where n is the number of elements of the resulting array.</p> <p> <strong>Syntax:</strong> </p> <pre> public static int[] copyOfRange(int[] original, int from, int to) </pre> <p>The method parses the three parameters:</p> <ul> <tr><td>original:</td> It is an array whose slice is to find. </tr><tr><td>from:</td> It is the start index. It must lie between 0 to the length of the given array. </tr><tr><td>to:</td> It is the end index. </tr></ul> <p>It throws the following exceptions:</p> <ul> <tr><td>ArrayIndexOutOfBoundsException:</td> If from is less than 0 or from is greater than the length of the specified array. </tr><tr><td>IllegalArgumentException:</td> If the parameter from is greater than to. </tr><tr><td>NullPointerException:</td> If the given array is null. </tr></ul> <p> <strong>SliceArrayExample2.java</strong> </p> <pre> import java.util.Arrays; public class SliceArrayExample2 { //function to get slice of a primitive array in Java public static int[] slice(int[] array, int startIndex, int endIndex) { // Get the slice of the Array int[] slicedArray = Arrays.copyOfRange(array, startIndex, endIndex); // return the slice return slicedArray; } public static void main(String args[]) { //get the array, startIndex and endIndex int[] array = {11, 23, 56, 90, 111, 901, 251, 800, 843}; int startIndex = 2, endIndex = 6; //get the slice of the array int[] sliceArray = slice(array, startIndex, endIndex + 1); //prints the slice of an array System.out.println(&apos;Slice of Array: &apos;+Arrays.toString(sliceArray)); } } </pre> <p> <strong>Output:</strong> </p> <pre> Slice of Array: [56, 90, 111, 901, 251] </pre> <h2>By Using Java 8 Stream</h2> <p>By using the following steps, we can find the slice of an array using the Java 8 Stream.</p> <ul> <li>First, find the startIndex and endIndex array.</li> <li>Convert the elements (that are in range) into Primitive Stream using range() method.</li> <li>Using the <strong>map()</strong> method map the specified elements from the specified array.</li> <li>By invoking the <strong>toArray()</strong> method, convert the mapped array into an array.</li> <li>Print the <strong>sliced</strong> </li> </ul> <p> <strong>SliceArrayExample3.java</strong> </p> <pre> import java.util.Arrays; import java.util.stream.IntStream; public class SliceArrayExample3 { //user defined function that finds the sslice of an specified array public static int[] findSlice(int[] array, int startIndex, int endIndex) { //getting the slice of an array and storing it in array slcarray[] //the range() method converts the elements into stream //getting the elments of the int stream using lambda expression //converting the mapped elements into sliced array using the toArray() method int[] slcarray = IntStream.range(startIndex, endIndex).map(i -&gt; array[i]).toArray(); //returns the slice of array return slcarray; } //main() method public static void main(String args[]) { //Get the array, startIndex and endIndex int[] array = {12, 45, 90, 55, 34, 100, 345, 897, 67, 123, 0, 789}; int startIndex = 5, endIndex = 10; //Get the slice of the array int[] slcarray = findSlice(array, startIndex, endIndex + 1); //Print the slice of the array System.out.println(&apos;Slice of array for the specified range is: &apos;+Arrays.toString(slcarray)); } } </pre> <p> <strong>Output:</strong> </p> <pre> Slice of array for the specified range is: [100, 345, 897, 67, 123, 0] </pre> <hr></slicedarray.length;>

Käyttämällä copyOfRange()-menetelmää

CopyOfRange()-menetelmä kuuluu Java Arrays -luokkaan. Se kopioi määritetyn alueen taulukosta äskettäin luotuun taulukkoon (slice array) ja palauttaa juuri luodun taulukon, joka sisältää määritetyn alueen alkuperäisestä taulukosta. Se ottaa Päällä) aika luoda viipalointi taulukosta ja Päällä) tilaa elementtien tallentamiseen, missä n on tuloksena olevan taulukon elementtien lukumäärä.

java-menetelmän ohittaminen

Syntaksi:

 public static int[] copyOfRange(int[] original, int from, int to) 

Menetelmä jäsentää kolme parametria:

    alkuperäinen:Se on taulukko, jonka siivu on löydettävä.alkaen:Se on aloitusindeksi. Sen on oltava 0:n ja annetun taulukon pituuden välillä.vastaanottajalle:Se on loppuindeksi.

Se antaa seuraavat poikkeukset:

merkkijono päivämäärään
    ArrayIndexOutOfBoundsException:Jos from on pienempi kuin 0 tai from on suurempi kuin määritetyn taulukon pituus.IllegalArgumentException:Jos parametri from on suurempi kuin to.NullPointerException:Jos annettu taulukko on tyhjä.

SliceArrayExample2.java

 import java.util.Arrays; public class SliceArrayExample2 { //function to get slice of a primitive array in Java public static int[] slice(int[] array, int startIndex, int endIndex) { // Get the slice of the Array int[] slicedArray = Arrays.copyOfRange(array, startIndex, endIndex); // return the slice return slicedArray; } public static void main(String args[]) { //get the array, startIndex and endIndex int[] array = {11, 23, 56, 90, 111, 901, 251, 800, 843}; int startIndex = 2, endIndex = 6; //get the slice of the array int[] sliceArray = slice(array, startIndex, endIndex + 1); //prints the slice of an array System.out.println(&apos;Slice of Array: &apos;+Arrays.toString(sliceArray)); } } 

Lähtö:

 Slice of Array: [56, 90, 111, 901, 251] 

Käyttämällä Java 8 Streamia

Seuraavien vaiheiden avulla voimme löytää taulukon osion Java 8 Streamin avulla.

  • Etsi ensin startIndex- ja endIndex-taulukko.
  • Muunna elementit (jotka ovat alueella) Primitiivivirraksi käyttämällä range()-menetelmää.
  • Käyttämällä kartta() menetelmä kuvaa määritetyt elementit määritetystä taulukosta.
  • Kutsumalla toArray() -menetelmällä muuntaa kartoitetun taulukon taulukoksi.
  • Tulosta viipaloitu

SliceArrayExample3.java

 import java.util.Arrays; import java.util.stream.IntStream; public class SliceArrayExample3 { //user defined function that finds the sslice of an specified array public static int[] findSlice(int[] array, int startIndex, int endIndex) { //getting the slice of an array and storing it in array slcarray[] //the range() method converts the elements into stream //getting the elments of the int stream using lambda expression //converting the mapped elements into sliced array using the toArray() method int[] slcarray = IntStream.range(startIndex, endIndex).map(i -&gt; array[i]).toArray(); //returns the slice of array return slcarray; } //main() method public static void main(String args[]) { //Get the array, startIndex and endIndex int[] array = {12, 45, 90, 55, 34, 100, 345, 897, 67, 123, 0, 789}; int startIndex = 5, endIndex = 10; //Get the slice of the array int[] slcarray = findSlice(array, startIndex, endIndex + 1); //Print the slice of the array System.out.println(&apos;Slice of array for the specified range is: &apos;+Arrays.toString(slcarray)); } } 

Lähtö:

 Slice of array for the specified range is: [100, 345, 897, 67, 123, 0]