Top Menu

Jump to content
Home
    • Projects
    • Work packages
    • News
    • Getting started
    • Introduction video
      Welcome to OpenProject
      Get a quick overview of project management and team collaboration with OpenProject. You can restart this video from the help menu.

    • Help and support
    • Upgrade to Enterprise edition
    • User guides
    • Videos
    • Shortcuts
    • Community forum
    • Professional support

    • Additional resources
    • Data privacy and security policy
    • Digital accessibility (DE)
    • OpenProject website
    • Security alerts / Newsletter
    • OpenProject blog
    • Release notes
    • Report a bug
    • Development roadmap
    • Add and edit translations
    • API documentation
  • Sign in
      Create a new account
      Forgot your password?

Side Menu

  • Overview
  • News
  • Forums
  • Wiki
    • Table of contents
      • Expanded. Click to collapseCollapsed. Click to showWiki
        • Expanded. Click to collapseCollapsed. Click to showBeispielprojekte
          • Hierarchy leafFlappy Box2D
          • Hierarchy leafFlappy Improved
          • Hierarchy leafHighscore Hibernate
          • Hierarchy leafHighscore Webservice + Anbindung mit Retrofit
        • Expanded. Click to collapseCollapsed. Click to showEntwicklung mit Java
          • Hierarchy leaf001) Grundlagen - Entwicklungsumgebung
          • Hierarchy leaf002) Erstes Programm
          • Hierarchy leaf003) Variablen und Datentypen
          • Expanded. Click to collapseCollapsed. Click to show004) Schleifen
            • Hierarchy leaf1) for - Zählschleife
            • Hierarchy leaf2) while
            • Hierarchy leaf3) do while
            • Hierarchy leaf4) for each
          • Hierarchy leaf005) Arrays
          • Hierarchy leaf006) Methoden
          • Expanded. Click to collapseCollapsed. Click to show007) Objektorientierte Programmierung
            • Hierarchy leaf001) Klasse
            • Hierarchy leaf002) Vererbung und Darstellung von Klassen in UML
            • Hierarchy leaf003) Abstrakte Klasse
            • Hierarchy leaf004) Design patterns
          • Expanded. Click to collapseCollapsed. Click to show008) Webservices
            • Hierarchy leaf01) REST - Representational State Transfer
            • Hierarchy leaf02) Minimaler Webservice
            • Hierarchy leaf03) Joke Webservice
            • Hierarchy leaf04) Highscore Service
        • Expanded. Click to collapseCollapsed. Click to showMatura - Vorbereitung
          • Hierarchy leaf01) Projektmanagement
          • Hierarchy leaf02) Objektorientierte Programmierung
          • Hierarchy leaf03) Modellierung und UML
          • Expanded. Click to collapseCollapsed. Click to show04) Design Patterns
            • Hierarchy leaf00) Generelle Konzepte
            • Hierarchy leafFactory(-Method) Pattern
            • Hierarchy leafObserver Pattern
            • Hierarchy leafSingleton Pattern
            • Hierarchy leafStrategy Pattern
          • Hierarchy leaf05) Algorithmen und Datenstrukturen
          • Hierarchy leaf06) Webtechnologien
          • Hierarchy leaf07) Webservices
          • Hierarchy leaf08) Softwarequalitätsmanagement
          • Hierarchy leaf09) Softwareentwicklungsmodelle
        • Expanded. Click to collapseCollapsed. Click to showProjekt Rahmenbedingungen
          • Expanded. Click to collapseCollapsed. Click to showRetrospektive
            • Hierarchy leafRetro - [Name Schüler_in]
        • Hierarchy leafReact Native
        • Expanded. Click to collapseCollapsed. Click to showÜbungen
          • Hierarchy leaf1) Basic
          • Expanded. Click to collapseCollapsed. Click to show2) Basic
            • Hierarchy leaf001) Methode ohne Rückabewert mit Parametern
            • Hierarchy leaf002) Methode mit Parametern und Rückgabewert
            • Hierarchy leaf003) Text umdrehen
            • Hierarchy leaf004) Text umdrehen 2
            • Hierarchy leaf005) Kommandozeile
            • Hierarchy leaf006) Kommandozeile 2
            • Hierarchy leaf007) Vererbung - Geometry
            • Hierarchy leaf008) File from String
            • Hierarchy leaf009) Rekursion - Summe
            • Hierarchy leaf010) Rekursion - File
          • Expanded. Click to collapseCollapsed. Click to showDatenstrukturen
            • Hierarchy leaf011) Binärbaum
            • Hierarchy leaf012) Binärbaum - Generics
            • Hierarchy leaf013) AVL - Tree
            • Hierarchy leaf014) Linked List - Einfach verkettete Liste
            • Hierarchy leaf015) Graph
            • Hierarchy leaf016) Dijkstras shortest path first
          • Expanded. Click to collapseCollapsed. Click to showJava - Kara
            • Hierarchy leafKara lernt schreiben
You are here:
  • Wiki
  • Entwicklung mit Java
  • 004) Schleifen
  • 4) for each

Content

4) for each

  • More
    • Print
    • Table of contents
  • Mit der for each Schleife kann komfortabel über Elemente von 005) Arrays und Collections (Eigentlich jeder nicht primitive Datentyp welcher das Interface Iterable implementiert) iteriert werden
    • Man erhält bei jedem Aufruf des Schleifenrumpfs eines der Elemente die enthalten sind

Erklärung der for each Schleife an hand eines Beispiels:

int[] arr = new int[]{1,2,3,4,5,6};
for(int tmp : arr) {
    System.out.println(tmp);
}

...es wird ein int Array mit den Variablen 1,2,3,4,5,6 erstellt und alle Zahlen werden ausgegeben

  • Datentyp der Variable
    • for(int tmp : arr) { System.out.println(tmp); }
    • Der Datentyp entspricht immer dem Datentyp eines Elements des Arrays oder der Collection
  • Name der Variable
    • for(int tmp : arr) { System.out.println(tmp); }
  • Über welches Array oder welche Collection soll iteriert werden
    • for(int tmp : arr) { System.out.println(tmp); }

Bsp 1)

String[] asdf = new String[] { "hallo", "hallo", "hallo" };
for(String s : asdf) {
   System.out.println(s);
}

...gibt 3x Hallo aus

Bsp 2)

Human[] humanArr = new Human[] { new Human("Max"), new Human("Alfons"), new Human("Xaver") };
for(Human human : humanArr) {
   System.out.println(human);
}

...deklariert ein Human Array und befüllt es mit 3 Humans, alle 3 Humans werden ausgegeben

import java.util.List;
import java.util.ArrayList;

...

List<Human> humanList = new ArrayList<Human>();
humanList.add(new Human("Max"));
humanList.add(new Human("Alfons"));
humanList.add(new Human("Xaver"));
for(Human human : humanArr) {
   System.out.println(human);
}

...selbes Beispiel wie oben, jedoch mit einer Liste anstatt einem Array

Loading...