SwapExample

SwapExample - println"Happy swap results" swapElementsHappy(array 0 1 println"array[0 =" array[0 println"array[1 ="

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
/* * SwapExample.java * ----------------- * This program shows an example of using arrays * when swapping values. */ import acm.program.*; public class SwapExample extends ConsoleProgram { public void run() { setFont("Courier New-24"); int[] array = new int[5]; array[0] = 1; array[1] = 2; println("Buggy swap results:"); swapElementsBuggy(array[0], array[1]); println("array[0] = " + array[0]); println("array[1] = " + array[1]);
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: println("Happy swap results:"); swapElementsHappy(array, 0, 1); println("array[0] = " + array[0]); println("array[1] = " + array[1]); } private void swapElementsBuggy(int x, int y) { int temp = x; x = y; y = temp; } private void swapElementsHappy(int arr, int pos1, int pos2) { int temp = arr[pos1]; arr[pos1] = arr[pos2]; arr[pos2] = temp; } }...
View Full Document

This note was uploaded on 02/18/2010 for the course CS 106A taught by Professor Sahami,m during the Fall '08 term at Stanford.

Ask a homework question - tutors are online