-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayListPracticeLab
182 lines (134 loc) · 5.74 KB
/
ArrayListPracticeLab
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import java.util.ArrayList;
public class ArrayListPracticeLab
{
// printMe is just a quick tool to check your work. Use it in conjunction with the test cases in main
public static void printMe(ArrayList<String> theList)
{
for (String str : theList)
System.out.print(str + ", ");
// I know, it prints an extra comma... live with it.
System.out.println();
}
/*
* convertArrayToList
*
* Write a method called convertArrayToList that accepts one parameter, an array of Strings, and returns
* an ArrayList containing those Strings
*/
public static ArrayList<String> convertArrayToList(String[] s) {
ArrayList<String> n = new ArrayList<String>();
for (String i : s) {
n.add(i);
}
return n;
}
/*
* maxLength
*
* Write a method maxLength that accepts one parameter, an ArrayList of Strings, and
* returns an int, the length of the longest string in the list. If your method is passed an
* empty list, it should return 0.
*/
public static int maxLength(ArrayList<String> s) {
int longest = 0;
for (String i : s) {
if (i.length() > longest) {
longest = i.length();
}
}
return longest;
}
/*
* swapPairs
*
* Write a method named swapPairs that switches the order of values in an ArrayList of Strings
* in a pairwise fashion. Your method should switch the order of the first two values,
* then switch the order of the next two, switch the order of the next two, and so on.
* swapPairs accepts one parameter, an ArrayList of Strings, and returns an ArrayList of Strings.
* swapPairs should not change the input ArrayList.
*
* For example, if the input list stores these values: {"four", "score", "and", "seven", "years", "ago"}
* your method should switch the first pair, "four", "score", the second pair, "and", "seven",
* and the third pair, "years", "ago", and the returned list should contain: {"score", "four", "seven", "and", "ago", "years"}
*
* If there are an odd number of values in the input list, the final element is not moved.
* For example, if the input list is: {"to", "be", "or", "not", "to", "be", "hamlet"}
* The returned list should contain {"be", "to", "not", "or", "be", "to", "hamlet"}
*/
public static ArrayList<String> swapPairs(ArrayList<String> s) {
ArrayList<String> n = new ArrayList<String>();
if(s.size() == 1) return s;
for (int i=0;i<s.size()-(s.size()%2);i++) {
if (i%2 == 0) n.add(s.get(i+1));
else n.add(s.get(i-1));
}
if (s.size() % 2 != 0) n.add(s.get(s.size()-1));
return n;
}
/*
* removeEvenLength
*
* Write a method named removeEvenLength that accepts one parameter, an ArrayList of Strings,
* and returns an ArrayList of Strings.
* The ArrayList returned by removeEvenLength contains all elements from the input parameter minus
* any Strings of even length.
* removeEvenLength should not change the input ArrayList.
*/
/*
* doubleList
*
* Write a method named doubleList that accepts one parameter, an ArrayList of Strings, and does not return anything.
* For each element in the input ArrayList, doubleList creates another element containing that same String.
*
* For example, if the input ArrayList contains the values {"how", "are", "you?"} before the method
* is called, it should contain the values {"how", "how", "are", "are", "you?", "you?"}
* after the method finishes executing.
*/
public static void main(String[] args)
{
// Declare an ArrayList of String named myList. Then fill it with: "this", "is", "it". Print myList using printMe().
// To test your maxLength method, convert the following to ArrayLists of Strings and
// pass them into your maxLength method. (You'll want to complete the convertArrayToList method first.)
// Expected output: 6, 27, 0
String[] test_max_1 = {"to", "be", "or", "not", "to", "be", "hamlet"};
String[] test_max_2 = {"Only one really long string"};
String[] test_max_3 = {};
//printMe( maxLength( convertArrayToList(test_max_1) ) );
System.out.println(maxLength(convertArrayToList(test_max_1)));
System.out.println(maxLength(convertArrayToList(test_max_2)));
System.out.println(maxLength(convertArrayToList(test_max_3)));
// To test your swapPairs method, convert the following to ArrayLists of Strings and
// pass them into your swapPairs method.
// Expected output:
// score, four, seven, and, ago, years
// love, I, programming!
// don't move me
// <blank>
String[] test_swap_1 = {"four", "score", "and", "seven", "years", "ago"};
String[] test_swap_2 = {"I", "love", "programming!"};
String[] test_swap_3 = {"don't move me"};
String[] test_swap_4 = {};
System.out.println(swapPairs(convertArrayToList(test_swap_1)));
System.out.println(swapPairs(convertArrayToList(test_swap_2)));
System.out.println(swapPairs(convertArrayToList(test_swap_3)));
System.out.println(swapPairs(convertArrayToList(test_swap_4)));
// To test your removeEvenLength method, convert the following to ArrayLists of Strings and
// pass them into your removeEvenLength method.
// Expected output:
// a
// Did, you, solve, what?
// <blank>
String[] test_rem_1 = {"This", "is", "a", "test"};
String[] test_rem_2 = {"Did", "you", "solve", "it", "or", "what?"};
String[] test_rem_3 = {};
// To test your doubleList method, convert the following to ArrayLists of Strings and
// pass them into your doubleList method.
// Expected output:
// [how, how, are, are, you?, you?]
// [One string only, One string only]
// <blank>
String[] test_doub_1 = {"how", "are", "you?"};
String[] test_doub_2 = {"One string only"};
String[] test_doub_3 = {};
}
}