must be in java: a) ( already completed for you ) Swap the first and last elemen
ID: 3745615 • Letter: M
Question
must be in java:
a) (already completed for you) Swap the first and last elements in the array list.
b) Shift all elements by one to the right and move the last element into the first position. For example,
1 4 9 16 25 would become 25 1 4 9 16.
c) Replace all even elements with 0.
d) Replace each element except the first and last by the larger of its two neighbors. For the example in part (b), your output would be 1 9 16 25 25.
e) Remove the middle element if the array list size is odd, or the middle two elements if the length is even. (You will need to test two different array lists here to make sure your method works on both even and odd length array lists).
f) Move all even elements to the front, otherwise preserving the order of the elements. For the example in part (b) your output should be 4 16 1 9 25
g) Return the second-largest element in the array list. (Your method should return this value, and your main method should print the value out). If your input is 3 25 50 50 22 note that 50 should be returned. 50 is both the largest and second largest value in this array list because there are 2 elements with value 50.
h) Return true if the array list is currently sorted in increasing order. (You can create a sorted ArrayList to use as input by starting with an empty Array List. Then use the add() method to add values in order one at a time). (Alternately you can use Collections.sort(); to sort your list. Use your ArrayList as a parameter to the sort() method). Make sure to test both a sorted array list and an unsorted array list as input. Your main method should print whether the array was in order or not.
i) Return true if the array list contains two adjacent duplicate elements.
j) Return true if the array list contains duplicate elements (which need not be adjacent)
starter code:
Explanation / Answer
import java.util.Collections; //need this one for sorting ArrayList
import java.util.Random;
import java.util.ArrayList;
public class ArrayListMethods {
// part a, done for you.
public static void swapFirstAndLast(ArrayList<Integer> a) {
// save first element
int temp = a.get(0);
// move last element to first position (0)
a.set(0, a.get(a.size() - 1));
// put temp value into last position (size() -1)
a.set(a.size() - 1, temp);
}
// part b, fill in this method
public static void shiftRight(ArrayList<Integer> a) {
//make temp variable to hold last element
int temp = a.get(a.size()-1);
//make a loop to run through the array list
for(int i = a.size()-1; i > 0; i--)
{
//set the last element to the value of the 2nd to last element
a.set(i,a.get(i-1));
}
//set the first element to be the last element
a.set(0, temp);
}
// part c, set all even elements to 0.
public static void setEvensToZero(ArrayList<Integer> a) {
for(int i=0; i < a.size(); i++ )
if( i%2== 0)
a.set(i, 0);
}
// part d, replace each element except the first and last by larger of two
// around it
public static void largerOfAdjacents(ArrayList<Integer> a) {
for(int i=1; i < a.size()-1; i++ )
{
if(a.get(i-1)>a.get(i+1))
a.set(i, a.get(i-1));
else
a.set(i, a.get(i+1));
}
}
// part e, remove middle el if odd length, else remove middle two els.
public static void removeMiddle(ArrayList<Integer> a) {
if(a.size()%2==0) {
a.remove(a.size()/2);
a.remove(a.size()/2-1);
}
else
a.remove(a.size());
}
// part f - move all evens to front
public static void moveEvensToFront(ArrayList<Integer> a) {
int temp=0;
int x=0;
for(int i=0; i < a.size(); i++){
if(a.get(i)%2 == 0){
for (int j=i; j>x; j--){
temp=a.get(i-1);
a.set(j-1, a.get(j));
a.set(j,temp);
}
x++;
}
}
}
// part g - return second largest el in array
public static int ret2ndLargest(ArrayList<Integer> a) {
int largest = a.get(0);
int secondLargest = a.get(0);
for (int i = 0; i < a.size(); i++) {
if (a.get(i) > largest) {
secondLargest = largest;
largest = a.get(i);
} else if (a.get(i) > secondLargest) {
secondLargest = a.get(i);
}
}
return secondLargest;
}
// part H - returns true if array is sorted in increasing order
public static boolean isSorted(ArrayList<Integer> a) {
boolean isSorted = false; // initially assume list is not sorted
ArrayList tmp = new ArrayList(a);
Collections.sort(tmp);
isSorted = tmp.equals(a);
return isSorted; // dummy value
}
// PART I - return true if array contains 2 adjacent duplicate values
public static boolean hasAdjDuplicates(ArrayList<Integer> a) {
boolean hasDup = false; // assume no duplicates, then search array
int length = a.size();
for(int i : a) {
for(int j = i + 1; j < length; j++) {
if(a.get(i) == a.get(j))
hasDup = true;
}
}
return hasDup;
}
// PART J - return true if array contains 2 duplicate values
// duplicates need not be adjacent to return true
public static boolean hasDuplicates(ArrayList<Integer> a) {
boolean hasDup = false; // assume no duplicates, then search array
int length = a.size();
for(int i = 0; i < length; i++) {
for(int j = i + 1; j < length; j++) {
if(a.get(i) == a.get(j))
hasDup = true;
}
}
return hasDup;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.