Gyakorlati alapok
Elemi programozási tételek egy 10 elemű tömbön
Bónusz I. - Elemsorrend fordítása
Bónuszként nézzük meg azt a rövid algoritmust, amelyik a tömbelemek sorrendjét fordítja meg (reverse).
import java.util.Random;
public class Main {
public static void main(String[] args) {
int [] tomb = new int[10];
Random rnd = new Random();
for(int i = 0; i < tomb.length; i++){
tomb[i] = rnd.nextInt(50) + 1;
System.out.print(tomb[i] + " ");
}
System.out.println();
int[] tombReverse = new int[tomb.length];
int j = tomb.length - 1;
for (int i = 0; i < tomb.length; i++){
tombReverse[i] = tomb[j];
System.out.print(tombReverse[i] + "
");
j--;
}
}
}
Végeredmény (például):
39 47 12 33 7 13 31 29 36 22
22 36 29 31 13 7 33 12 47 39