public class ReverseTable {
public static Integer counter;
public static void main(String args[]) throws FileNotFoundException {
File input = new File("in.txt");
Scanner file = new Scanner(input);
ArrayList<Integer> table = new ArrayList<>();
while(file.hasNextInt()) {
table.add(file.nextInt());
}
Integer tab[] = table.toArray(new Integer[table.size()]);
System.out.print("Oryginalna tablica: ");
for(int i : tab) {
System.out.print(i + ", ");
}
System.out.println();
System.out.println();
System.out.print("Odwrocenie iteracyjne: ");
Integer iteRevTab[] = iterationReverse(tab.clone());
for(int i : iteRevTab) {
System.out.print(i + ", ");
}
System.out.println("Ilosc operacji elementarych: " + counter);
}
public static Integer[] iterationReverse(Integer tab[]) {
int lastElement = tab.length - 1;
int buffor;
int operationCounter = 0;
for(int firstElement = 0; firstElement < (tab.length - 1) / 2; firstElement++) {
buffor = tab[firstElement];
tab[firstElement] = tab[lastElement];
tab[lastElement] = buffor;
lastElement--;
operationCounter++;
}
counter = new Integer(operationCounter);
return tab;
}
}
Integer to tzw. klasa immutable, czyli niezmienialna (podobnie jak String - nie posiadają metod set, czyli setterów lub jak to woli - mutatorów). Nie możesz zmienić jej wartości, gdy już raz została utworzona.
Rozwiązaniem w tym przypadku jest skorzystanie z pola statycznego counter, ponieważ gdzieś musisz przechować pożądaną wartość Integer. W metodzie iterationReverse(...) dodałem zmienną int operationCounter, która zlicza liczbę operacji i po wszystkim inicjalizuję zmienną statyczną wartością zmiennej operationCounter. Potem wystarczy wyświetlić wartość zmiennej statycznej counter.