forked from ryanseapy/Schoolprojects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayListMethods.java
114 lines (100 loc) · 3.43 KB
/
ArrayListMethods.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
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
//
//Complete the class ArrayListMethods. It consists of four short methods to manipulate an array list of strings.
//The method header and javadoc are given to you.
//
// Need help starting this question? In the lesson titled
// "Starting points: Problem Set Questions", go to the
// problem titled "Problem Set 6 - Question 1" for some tips on
// how to begin.
//
import java.util.ArrayList;
public class ArrayListMethods
{
ArrayList<String> list; //instance variable
/**
* Constructor for objects of class ArrayListMethods
*/
public ArrayListMethods(ArrayList<String> arrayList)
{
// initialise instance variables
list = arrayList;
}
/**
* Determines if the array list is sorted (do not sort)
* When Strings are sorted, they are in alphabetical order
* Use the compareTo method to determine which string comes first
* You can look at the String compareTo method in the Java API
* @return true if the array list is sorted else false.
*/
public boolean isSorted()
{
boolean sorted = true;
for(int i =0; i<(list.size()-1);i++)
{
if(list.get(i).compareTo(list.get(i+1))==1)
{
sorted = false;
}
}
// TODO: Determine if the array is sorted.
return sorted;
}
/**
* Replaces all but the first and last elements with the larger of its two neighbors
* You can use the compareTo() method to determine which string is larger (larger in alphabetical
* order).
* Example: if the list is originally
* ["cat", "ape", "dog", "horse", "zebra"]
* after this method it should be:
* ["cat", "dog", "horse", "zebra", "zebra"]
*/
public void replaceWithLargerNeighbor()
{
for(int i =1; i<list.size()-1;i++)
{
if(list.get(i-1).compareTo(list.get(i+1))==-1)
{
list.set(i, list.get(i-1));
}
else if(list.get(i+1).compareTo(list.get(i-1))==1)
{
list.set(i, list.get(i+1));
}
}
// TODO: Replace all but the first and last elements with the larger of its to neighbors
}
/**
* Gets the number of duplicates in the list.
* Be careful to only count each duplicate once. Start at index 0. (Does it match any of the other elements?)
* Get the next word. It is at index i. (Does it match any of the words with index > i?)
* @return the number of duplicate words in the list
*/
public int countDuplicates()
{
int duplicates = 0;
// TODO: Write the code to get the number of duplicates in the list
return duplicates;
}
/**
* Moves any word that starts with x, y, or z to the front of the ArrayList, but
* otherwise preserves the order
* Example: if the list is orginially
* ["ape", "dog", "xantus", "zebra", "cat", "yak"]
* after this method is called it should be
* ["xantus", "zebra", "yak", "ape", "dog", "cat"]
*/
public void xyzToFront()
{
int insertAt = 0;
// TODO: Move any word that starts with x, y, or z to the front of the ArrayList, but otherwise preserves the order
}
/**
* gets the string representation of this array list
* @returns the string representation of this array list in
* standard collection format
*/
public String toString()
{
return list.toString();
}
}