// Helene Martin, CSE 142 // Reference semantics and String traversal import java.util.*; public class ReferenceSemantics { public static void main(String[] args) { int[] grades = {45, 68, 98, 100}; // use Jeliot to visualize this (http://cs.joensuu.fi/jeliot/) int[] scores = grades; scores[2] = 12; // both are the same! scores and grades hold a reference to the same array System.out.println(Arrays.toString(scores)); System.out.println(Arrays.toString(grades)); System.out.println(reverse("cse142")); } // given a String, returns a String with letters in reverse order // can't work exactly like array reversal because Strings are immutable // build up a new string through concatenation // array String // .length .length() // [i] .charAt(i) public static String reverse(String initial) { String result = ""; // start with an empty string for (int i = 0; i < initial.length(); i++) { result = result + initial.charAt(initial.length() - i - 1); // cumulative sum pattern } return result; } }