logo

Kako ponoviti seznam v Javi

V Javi, Seznam je vmesnik za Ogrodje zbirke . Omogoča nam vzdrževanje urejene zbirke predmetov. Izvedbeni razredi vmesnika List so ArrayList, LinkedList, Stack , in Vektor . ArrayList in LinkedList se pogosto uporabljata v Java . V tem razdelku se bomo naučili kako ponoviti seznam v Javi . V tem razdelku bomo uporabljali ArrayList .

Java za Loop

  1. Osnovno za Loop
  2. Izboljšano za Loop

Iteratorji Java

  1. Iterator
  2. ListIterator

Metoda Java forEach

  1. Iterable.forEach()
  2. Stream.forEach()

Java za Loop

Osnovno za Loop

Java za zanko je najpogostejša zanka za nadzor pretoka za iteracijo. Zanka for vsebuje spremenljivko, ki deluje kot indeksno število. Izvaja se, dokler se celoten seznam ne ponovi.

Sintaksa:

 for(initialization; condition; increment or decrement) { //body of the loop } 

IterateListExample1.java

 import java.util.*; public class IterateListExample1 { public static void main(String args[]) { //defining a List List city = Arrays.asList(&apos;Boston&apos;, &apos;San Diego&apos;, &apos;Las Vegas&apos;, &apos;Houston&apos;, &apos;Miami&apos;, &apos;Austin&apos;); //iterate list using for loop for (int i = 0; i <city.size(); i++) { prints the elements of list system.out.println(city.get(i)); } < pre> <p> <strong>Output</strong> </p> <pre> Boston San Diego Las Vegas Houston Miami Austin </pre> <h3>Enhanced for Loop</h3> <p>It is similar to the basic for loop. It is compact, easy, and readable. It is widely used to perform traversal over the List. It is easy in comparison to basic for loop.</p> <p> <strong>Syntax:</strong> </p> <pre> for(data_type variable : array | collection) { //body of the loop } </pre> <p> <strong>IterateListExample2.java</strong> </p> <pre> import java.util.*; public class IterateListExample2 { public static void main(String args[]) { //defining a List List city = Arrays.asList(&apos;Boston&apos;, &apos;San Diego&apos;, &apos;Las Vegas&apos;, &apos;Houston&apos;, &apos;Miami&apos;, &apos;Austin&apos;); //iteration over List using enhanced for loop for (String cities : city) { //prints the elements of the List System.out.println(cities); } } } </pre> <p> <strong>Output</strong> </p> <pre> Boston San Diego Las Vegas Houston Miami Austin </pre> <h2>Java Iterator</h2> <h3>Iterator</h3> <p> <a href="/iterator-java">Java provides an interface Iterator</a> to <strong>iterate</strong> over the Collections, such as List, Map, etc. It contains two key methods next() and hasNaxt() that allows us to perform an iteration over the List.</p> <p> <strong>next():</strong> The next() method perform the iteration in forward order. It returns the next element in the List. It throws <strong>NoSuchElementException</strong> if the iteration does not contain the next element in the List. This method may be called repeatedly to iterate through the list, or intermixed with calls to previous() to go back and forth.</p> <p> <strong>Syntax:</strong> </p> <pre> E next() </pre> <p> <strong>hasNext():</strong> The hasNext() method helps us to find the last element of the List. It checks if the List has the next element or not. If the hasNext() method gets the element during traversing in the forward direction, returns true, else returns false and terminate the execution.</p> <p> <strong>Syntax:</strong> </p> <pre> boolean hasNext() </pre> <p> <strong>IterateListExample3.java</strong> </p> <pre> import java.util.*; public class IterateListExample3 { public static void main(String args[]) { //defining a List List city = Arrays.asList(&apos;Boston&apos;, &apos;San Diego&apos;, &apos;Las Vegas&apos;, &apos;Houston&apos;, &apos;Miami&apos;, &apos;Austin&apos;); //iterate List using enhances for loop Iterator cityIterator = city.iterator(); //iterator over List using while loop while(cityIterator.hasNext()) { System.out.println(cityIterator.next()); } } } </pre> <p> <strong>Output</strong> </p> <pre> Boston San Diego Las Vegas Houston Miami Austin </pre> <h3>ListIterator</h3> <p>The ListIterator is also an interface that belongs to java.util package. It extends <strong>Iterator</strong> interface. It allows us to iterate over the List either in forward or backward order. The forward iteration over the List provides the same mechanism, as used by the Iterator. We use the next() and hasNext() method of the Iterator interface to iterate over the List.</p> <p> <strong>IterateListExample4.java</strong> </p> <pre> import java.util.*; public class IterateListExample4 { public static void main(String args[]) { //defining a List List city = Arrays.asList(&apos;Boston&apos;, &apos;San Diego&apos;, &apos;Las Vegas&apos;, &apos;Houston&apos;, &apos;Miami&apos;, &apos;Austin&apos;); //iterate List using the ListIterator ListIterator listIterator = city.listIterator(); while(listIterator.hasNext()) { //prints the elements of the List System.out.println(listIterator.next()); } } } </pre> <p> <strong>Output</strong> </p> <pre> Boston San Diego Las Vegas Houston Miami Austin </pre> <h2>Java forEach Method</h2> <h3>Iterable.forEach()</h3> <p>The Iterable interface provides forEach() method to iterate over the List. It is available since Java 8. It performs the specified action for each element until all elements have been processed or the action throws an exception. It also accepts Lambda expressions as a parameter.</p> <p> <strong>Syntax:</strong> </p> <pre> default void forEach(Consumer action) </pre> <p>The default implementation behaves like:</p> <pre> for (T t : this) action.accept(t); </pre> <p>It accepts <strong>action</strong> as a parameter that is <strong>non-interfering</strong> (means that the data source is not modified at all during the execution of the stream pipeline) action to perform on the elements. It throws <strong>NullPointerException</strong> if the specified action is null.</p> <p>The <strong>Consumer</strong> is a functional interface that can be used as the assignment target for a lambda expression or method reference. T is the type of input to the operation. It represents an operation that accepts a single input argument and returns no result.</p> <p> <strong>IterateListExample5.java</strong> </p> <pre> import java.util.*; public class IterateListExample5 { public static void main(String args[]) { //defining a List List city = Arrays.asList(&apos;Boston&apos;, &apos;San Diego&apos;, &apos;Las Vegas&apos;, &apos;Houston&apos;, &apos;Miami&apos;, &apos;Austin&apos;); //iterate List using forEach city.forEach(System.out::println); } } </pre> <p> <strong>Output</strong> </p> <pre> Boston San Diego Las Vegas Houston Miami Austin </pre> <h3>Stream.forEach()</h3> <p>Java Stream interface allows us to convert the List values into a stream. With the help of Stream interface we can access operations like forEach(), map(), and filter().</p> <p> <strong>Syntax:</strong> </p> <pre> void forEach(Consumer action) </pre> <p>It accepts <strong>action</strong> as a parameter that is <strong>non-interfering</strong> (means that the data source is not modified at all during the execution of the stream pipeline) action to perform on the elements.</p> <p>The <strong>Consumer</strong> is a functional interface that can be used as the assignment target for a lambda expression or method reference. T is the type of input to the operation. It represents an operation that accepts a single input argument and returns no result.</p> <p> <strong>IterateListExample5.java</strong> </p> <pre> import java.util.*; public class IterateListExample5 { public static void main(String args[]) { //defining a List List city = Arrays.asList(&apos;Boston&apos;, &apos;San Diego&apos;, &apos;Las Vegas&apos;, &apos;Houston&apos;, &apos;Miami&apos;, &apos;Austin&apos;); //iterate List using forEach loop city.stream().forEach((c) -&gt; System.out.println(c)); } } </pre> <p> <strong>Output</strong> </p> <pre> Boston San Diego Las Vegas Houston Miami Austin </pre> <hr></city.size();>

Izboljšano za Loop

Podobna je osnovni zanki for. Je kompakten, enostaven in berljiv. Pogosto se uporablja za izvajanje prečkanja seznama. V primerjavi z osnovno zanko for je enostavna.

Sintaksa:

 for(data_type variable : array | collection) { //body of the loop } 

IterateListExample2.java

 import java.util.*; public class IterateListExample2 { public static void main(String args[]) { //defining a List List city = Arrays.asList(&apos;Boston&apos;, &apos;San Diego&apos;, &apos;Las Vegas&apos;, &apos;Houston&apos;, &apos;Miami&apos;, &apos;Austin&apos;); //iteration over List using enhanced for loop for (String cities : city) { //prints the elements of the List System.out.println(cities); } } } 

Izhod

java skener
 Boston San Diego Las Vegas Houston Miami Austin 

Java Iterator

Iterator

Java ponuja vmesnik Iterator do ponoviti nad zbirkami, kot so seznam, zemljevid itd. Vsebuje dve ključni metodi next() in hasNaxt(), ki nam omogočata izvedbo iteracije nad seznamom.

Naslednji(): Metoda next() izvede ponovitev v naprej. Vrne naslednji element na seznamu. Vrže NoSuchElementException če ponovitev ne vsebuje naslednjega elementa na seznamu. To metodo lahko kličete večkrat za ponavljanje po seznamu ali pa jo pomešate s klici prejšnjega(), da se pomikate naprej in nazaj.

Sintaksa:

 E next() 

hasNext(): Metoda hasNext() nam pomaga najti zadnji element seznama. Preveri, ali seznam vsebuje naslednji element ali ne. Če metoda hasNext() pridobi element med prečkanjem v smeri naprej, vrne true, sicer vrne false in prekine izvajanje.

Sintaksa:

 boolean hasNext() 

IterateListExample3.java

 import java.util.*; public class IterateListExample3 { public static void main(String args[]) { //defining a List List city = Arrays.asList(&apos;Boston&apos;, &apos;San Diego&apos;, &apos;Las Vegas&apos;, &apos;Houston&apos;, &apos;Miami&apos;, &apos;Austin&apos;); //iterate List using enhances for loop Iterator cityIterator = city.iterator(); //iterator over List using while loop while(cityIterator.hasNext()) { System.out.println(cityIterator.next()); } } } 

Izhod

 Boston San Diego Las Vegas Houston Miami Austin 

ListIterator

ListIterator je tudi vmesnik, ki pripada paketu java.util. Razširja se Iterator vmesnik. Omogoča nam iteracijo po seznamu v vrstnem redu naprej ali nazaj. Ponavljanje naprej po seznamu zagotavlja enak mehanizem, kot ga uporablja iterator. Za ponavljanje seznama uporabljamo metodi next() in hasNext() vmesnika Iterator.

IterateListExample4.java

 import java.util.*; public class IterateListExample4 { public static void main(String args[]) { //defining a List List city = Arrays.asList(&apos;Boston&apos;, &apos;San Diego&apos;, &apos;Las Vegas&apos;, &apos;Houston&apos;, &apos;Miami&apos;, &apos;Austin&apos;); //iterate List using the ListIterator ListIterator listIterator = city.listIterator(); while(listIterator.hasNext()) { //prints the elements of the List System.out.println(listIterator.next()); } } } 

Izhod

kako izstopiti iz zanke while java
 Boston San Diego Las Vegas Houston Miami Austin 

Metoda Java forEach

Iterable.forEach()

Vmesnik Iterable ponuja metodo forEach() za ponavljanje seznama. Na voljo je od Jave 8 naprej. Izvaja določeno dejanje za vsak element, dokler niso vsi elementi obdelani ali dejanje vrže izjemo. Sprejema tudi lambda izraze kot parameter.

Sintaksa:

 default void forEach(Consumer action) 

Privzeta izvedba se obnaša kot:

 for (T t : this) action.accept(t); 

Sprejema ukrepanje kot parameter, ki je nevmešavanje (pomeni, da se vir podatkov med izvajanjem tokovnega cevovoda sploh ne spremeni) dejanje za izvedbo na elementih. Vrže NullPointerException če je navedeno dejanje ničelno.

The Potrošnik je funkcionalni vmesnik, ki se lahko uporablja kot cilj dodelitve za lambda izraz ali referenco metode. T je vrsta vnosa v operacijo. Predstavlja operacijo, ki sprejme en vhodni argument in ne vrne nobenega rezultata.

IterateListExample5.java

 import java.util.*; public class IterateListExample5 { public static void main(String args[]) { //defining a List List city = Arrays.asList(&apos;Boston&apos;, &apos;San Diego&apos;, &apos;Las Vegas&apos;, &apos;Houston&apos;, &apos;Miami&apos;, &apos;Austin&apos;); //iterate List using forEach city.forEach(System.out::println); } } 

Izhod

vrste združevanj v rdbms
 Boston San Diego Las Vegas Houston Miami Austin 

Stream.forEach()

Vmesnik Java Stream nam omogoča pretvorbo vrednosti seznama v tok. S pomočjo vmesnika Stream lahko dostopamo do operacij, kot so forEach(), map() in filter().

Sintaksa:

 void forEach(Consumer action) 

Sprejema ukrepanje kot parameter, ki je nevmešavanje (pomeni, da se vir podatkov med izvajanjem tokovnega cevovoda sploh ne spremeni) dejanje za izvedbo na elementih.

The Potrošnik je funkcionalni vmesnik, ki se lahko uporablja kot cilj dodelitve za lambda izraz ali referenco metode. T je vrsta vnosa v operacijo. Predstavlja operacijo, ki sprejme en vhodni argument in ne vrne nobenega rezultata.

IterateListExample5.java

 import java.util.*; public class IterateListExample5 { public static void main(String args[]) { //defining a List List city = Arrays.asList(&apos;Boston&apos;, &apos;San Diego&apos;, &apos;Las Vegas&apos;, &apos;Houston&apos;, &apos;Miami&apos;, &apos;Austin&apos;); //iterate List using forEach loop city.stream().forEach((c) -&gt; System.out.println(c)); } } 

Izhod

 Boston San Diego Las Vegas Houston Miami Austin