generated from ZipCodeCore/OldTexasCode2
-
Notifications
You must be signed in to change notification settings - Fork 11
/
EnhancedFor.java
69 lines (60 loc) · 1.63 KB
/
EnhancedFor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
public class EnhancedFor
{
public static void main(String[] args)
{ int[] list ={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sum = sumListEnhanced(list);
System.out.println("Sum of elements in list: " + sum);
System.out.println("Original List");
printList(list);
System.out.println("Calling addOne");
addOne(list);
System.out.println("List after call to addOne");
printList(list);
System.out.println("Calling addOneError");
addOneError(list);
System.out.println("List after call to addOneError. Note elements of list did not change.");
printList(list);
}
// pre: list != null
// post: return sum of elements
// uses enhanced for loop
public static int sumListEnhanced(int[] list)
{ int total = 0;
for(int val : list)
{ total += val;
}
return total;
}
// pre: list != null
// post: return sum of elements
// use traditional for loop
public static int sumListOld(int[] list)
{ int total = 0;
for(int i = 0; i < list.length; i++)
{ total += list[i];
System.out.println( list[i] );
}
return total;
}
// pre: list != null
// post: none.
// The code appears to add one to every element in the list, but does not
public static void addOneError(int[] list)
{ for(int val : list)
{ val = val + 1;
}
}
// pre: list != null
// post: adds one to every element of list
public static void addOne(int[] list)
{ for(int i = 0; i < list.length; i++)
{ list[i]++;
}
}
public static void printList(int[] list)
{ System.out.println("index, value");
for(int i = 0; i < list.length; i++)
{ System.out.println(i + ", " + list[i]);
}
}
}