Java Reference
In-Depth Information
3.
Write a method called
removeEvenLength
that takes an
ArrayList
of
String
s as a parameter and removes all of
the
String
s of even length from the list.
4.
Write a method called
doubleList
that takes an
ArrayList
of
String
s as a parameter and replaces every
String
with two of that same
String
. For example, if the list stores the values (“how”, “are”, “you?”) before the method is
called, it should store the values (“how”, “how”, “are”, “are”, “you?”, “you?”) after the method finishes executing.
5.
Write a method called
scaleByK
that takes an
ArrayList
of integers as a parameter and replaces every integer of
value
K
with
K
copies of itself. For example, if the list stores the values (4, 1, 2, 0, 3) before the method is called, it
should store the values (4, 4, 4, 4, 1, 2, 2, 3, 3, 3) after the method finishes executing. Zeroes and negative numbers
should be removed from the list by this method.
6.
Write a method called
minToFront
that takes an
ArrayList
of integers as a parameter and moves the minimum
value in the list to the front, otherwise preserving the order of the elements. For example, if a variable called
list
stores (3, 8, 92, 4, 2, 17, 9), the value 2 is the minimum, so your method should modify the list to store the values
(2, 3, 8, 92, 4, 17, 9).
7.
Write a method called
removeDuplicates
that takes as a parameter a sorted
ArrayList
of
String
s and eliminates
any duplicates from the list. For example, if the list stores the values (“be”, “be”, “is”, “not”, “or”, “question”, “that”,
“the”, “to”, “to”) before the method is called, it should store the values (“be”, “is”, “not”, “or”, “question”, “that”,
“the”, “to”) after the method finishes executing. Because the values will be sorted, all of the duplicates will be
grouped together. Assume that the
ArrayList
contains only
String
values, but keep in mind that it might be empty.
8.
Write a method called
removeZeroes
that takes as a parameter an
ArrayList
of integers and eliminates any
occurrences of the number
0
from the list. For example, if the list stores the values (0, 7, 2, 0, 0, 4, 0) before the
method is called, it should store the values (7, 2, 4) after the method finishes executing.
9.
Write a method called
rangeBetweenZeroes
that takes as a parameter an
ArrayList
of integers and returns the
number of indexes apart the two furthest occurrences of the number
0
are. For example, if the list stores the
values (7, 2, 0, 0, 4, 0, 9, 0, 6, 4, 8) when the method is called, it should return
6
, because the occurrences of
0
that
are furthest apart are at indexes 2 and 7, and the range 2 through 7 has six elements. If only one
0
occurs in the list,
your method should return
1
. If no
0
s occur, your method should return
0
.
10.
Write a method called
removeInRange
that accepts three parameters, an
ArrayList
of
String
s, a beginning
String
, and an ending
String
, and removes from the list any
String
s that fall alphabetically between the start
and end
String
s. For example, if the method is passed a list containing the elements (“to”, “be”, “or”, “not”, “to”,
“be”, “that”, “is”, “the”, “question”), “free” as the start
String
, and “rich” as the end
String
, the list's elements
should be changed to (“to”, “be”, “to”, “be”, “that”, “the”). The “or”, “not”, “is”, and “question” should be removed
because they occur alphabetically between “free” and “rich”. You may assume that the start
String
alphabetically
precedes the ending
String
.
11.
Write a method called
stutter
that accepts an
ArrayList
of strings as a parameter and that replaces every string
with two of that string. For example, if the list stores the values [“how”, “are”, “you?”], after the call it should store
[“how”, “how”, “are”, “are”, “you?”, “you?”].
12.
Write a method called
markLength4
that accepts an
ArrayList
of strings as a parameter and that places a string of
four asterisks
"****"
in front of every string of length 4. For example, suppose that a variable called
list
contains the values [“this”, “is”, “lots”, “of”, “fun”, “for”, “Java”, “coders”]. The call of
markLength4(list);
should change the list to store the values [“****”, “this”, “is”, “****”, “lots”, “of”, “fun”, “for”, “****”, “Java”,
“coders”].
Search WWH ::
Custom Search