Les boucles for classiques utilisent les index pour boucler sur les tableaux. Elles doivent être utilisées lorsque qu'on ne souhaite pas forcément accéder à tous les éléments ou lorsqu'il est nécessaire d'utiliser les index. Elle est moins pratique et entraine plus de bug
Created
September 28, 2023 11:56
-
-
Save nherbaut/4d8a3fb1d8edcbe5a1113df248f85d85 to your computer and use it in GitHub Desktop.
L2.2.3 Les boucles for classiques
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Map; | |
| import java.util.HashMap; | |
| public class HelloWorld | |
| { | |
| public void runHello(){ | |
| FileResource f = new FileResource("hello_unicode.txt"); | |
| String[] files = f.lines(); | |
| for(int i=0;i<files.length;i++){ | |
| System.out.println(files[i]); | |
| } | |
| int j=0; | |
| for(int i=0;i<100;i+=2){ | |
| j+=i; | |
| } | |
| System.out.println(j); | |
| } | |
| public static void main(String args[]){ | |
| HelloWorld hw = new HelloWorld(); | |
| hw.runHello(); | |
| } | |
| class FileResource{ | |
| private final static Map<String,String> content; | |
| static { | |
| content=new HashMap<>(); | |
| content.put("hello_unicode.txt","1,2\n4,6\n7,9"); | |
| } | |
| private final String resource; | |
| public FileResource(String ref){ | |
| resource=content.get(ref); | |
| } | |
| public String[] lines(){ | |
| return resource.split("\n"); | |
| } | |
| } | |
| } | |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
execute me on https://java.miage.dev/?gistId=4d8a3fb1d8edcbe5a1113df248f85d85