Java Loop Kupfuura Chinyorwa

Muchidzidzo ichi, tinotarisa nzira shanu dzakasiyana dzatinogona kurerutsa kuburikidza neArrayList muJava. Sezvo zva Java 8, tinogona kushandisa nzira yeEach pamwe neiyo iterator kirasi kusunungura pamusoro peArrayList.

Kusunungura pamusoro peArrayList

Pane nzira shanu dzakasiyana dzekusunungura pamusoro peArrayList

  1. Yakasarudzika YeRopu
  2. Yakafambiswa Kune Loop
  3. Iterator
  4. Ipo Loop
  5. ForEach (Java 8)

Kutanga, ngatigadzirei ArrayList yekushandisa mumienzaniso yekurembera:

import java.util.ArrayList; import java.util.List; public class LoopOverArrayExamples {
private List fruitBasket = new ArrayList(0);
public void addFruitsToBasket() {
fruitBasket.add('Apple');
fruitBasket.add('Banana');
fruitBasket.add('Pear');
fruitBasket.add('Mango');
} }

Inoenderana:

Uchishandisa Classic YeRoop

for (int i=0; iSystem.out.println(fruitBasket.get(i)); }

Yakafambiswa Kune Loop

for(String fruit : fruitBasket) {
System.out.println(fruit); }

Uchishandisa Iterator

Iterator fruitIterator = fruitBasket.iterator(); while (fruitIterator.hasNext()) {
System.out.println(fruitIterator.next()); }

Uchishandisa Ipo Loop

int i = 0; while (i < fruitBasket.size()) {
System.out.println(fruitBasket.get(i));
i++; }

ForEach (Java 8)

fruitBasket.forEach( (fruit) ->
System.out.println(fruit) );