Loading search index…
No recent searches
No results for "Query here"
Here we use range to sum the numbers in a slice. Arrays work like this too.
range
import java.util.Arrays; import java.util.List; public class RangeExample { public static void main(String[] args) { List<Integer> nums = Arrays.asList(2, 3, 4); int sum = 0; for (int num : nums) { sum += num; } System.out.println("sum: " + sum); for (int i = 0; i < nums.size(); i++) { if (nums.get(i) == 3) { System.out.println("index: " + i); } } java.util.Map<String, String> kvs = java.util.Map.of("a", "apple", "b", "banana"); for (java.util.Map.Entry<String, String> entry : kvs.entrySet()) { System.out.println(entry.getKey() + " -> " + entry.getValue()); } for (String k : kvs.keySet()) { System.out.println("key: " + k); } String str = "go"; for (int i = 0; i < str.length(); i++) { System.out.println(i + " " + (int) str.charAt(i)); } } }
To run the program, compile the code into a .class file and then use java to execute it.
.class
java
$ javac RangeExample.java $ java RangeExample sum: 9 index: 1 a -> apple b -> banana key: a key: b 0 103 1 111