-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursive.java
More file actions
100 lines (64 loc) · 1.94 KB
/
Recursive.java
File metadata and controls
100 lines (64 loc) · 1.94 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package lab8;
import java.util.ArrayList;
class Recursive
{
private static ArrayList<Integer> list = new ArrayList<Integer>();
private static ArrayList<Integer> reversedList = new ArrayList<Integer>();
// this one builds a list containing values from 1 to n
public static ArrayList<Integer> buildList(int n)
{
// write this in terms of a recursive call using a smaller n
ArrayList<Integer> tempList = null;
if (n <= 0) // The smallest list we can make
{
}
else // All other size lists are created here
{
}
return tempList;
}
// this one reverses a list in-place
public static ArrayList<Integer> reverse(ArrayList<Integer> lst)
{
// write this in terms of a recursive call using a smaller lst
if (// The list is empty or has one element)
{// Return the list as is – no need to reverse!
} else {
// Use the solution to a smaller version of the problem
// to solve the general problem
}
return reversedList;
}
// return the sum of all Integers in the ArrayList
// this should not change the lst argument
public static int add(ArrayList<Integer> lst)
{
return add(lst,0);
}
// Print out all the contents of the argument
// this should not change the lst argument
public static void print(ArrayList<Integer> lst)
{
print(lst,0);
return;
}
private static int add (ArrayList<Integer> lst, int index)
{
// think of the input is the inclusive sublist of elements from index
// to lst.size(). make this sublist shorter in the recursive call
// by incrementing index
}
private static void print (ArrayList<Integer> lst, int index)
{
// write this in the same way as add, above
}
public static void main(String[] args)
{
ArrayList<Integer> lst = Recursive.buildList(5);
System.out.println(lst);
reverse(lst);
System.out.println(lst);
Recursive.print(lst);
System.out.println(Recursive.add(lst));
}
}