Hello I am a beginner in Java and am having issues with ArrayLists and formulati
ID: 3561786 • Letter: H
Question
Hello I am a beginner in Java and am having issues with ArrayLists and formulating methods. I had little issue with formulating methods for Arrays, but ArrayLists seems to be difficult for me for some reason. Here's what needs to be done.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).
And here is my code so far.
import java.util.Collections; //need this one for sorting ArrayList import java.util.Random; import java.util.ArrayList;
public class ArrayListMethods { //ArrayList to hold values to work on for lab private ArrayList<Integer> listValues;
//ArrayList constructor public ArrayListMethods(ArrayList<Integer> initialValues) { //note this shows how to make a copy of an array list //simply pass the old list in as an argument to //the ArrayList constructor listValues = new ArrayList<Integer>(initialValues); } //set listValues to list passed as parm //use this method to "reset" our ArrayList data before each test public void setArrayList(ArrayList<Integer> newValues) { listValues = new ArrayList<Integer>(newValues); } //method to print the current values in the ArrayList //or you can simply use println() with the array list as the //parameter public void printArrayList() { System.out.println(listValues); } public void swapFirstAndLast() { //save first element int temp = listValues.get(0); //move last element to first position (0) listValues.set(0, listValues.get(listValues.size()-1)); //put temp value into last position (size() -1) listValues.set(listValues.size()-1, temp); } //part b public void shiftRight() { } //part c, set all even elements to 0. public void setEvensToZero() { } //part d, replace each element except the first and last by larger of two //around it public void largerOfAdjacents() { } //part e, remove middle el if odd length, else remove middle two els. public void removeMiddle() { }
Hello I am a beginner in Java and am having issues with ArrayLists and formulating methods. I had little issue with formulating methods for Arrays, but ArrayLists seems to be difficult for me for some reason. Here's what needs to be done.
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).
And here is my code so far.
import java.util.Collections; //need this one for sorting ArrayList import java.util.Random; import java.util.ArrayList;
public class ArrayListMethods { //ArrayList to hold values to work on for lab private ArrayList<Integer> listValues;
//ArrayList constructor public ArrayListMethods(ArrayList<Integer> initialValues) { //note this shows how to make a copy of an array list //simply pass the old list in as an argument to //the ArrayList constructor listValues = new ArrayList<Integer>(initialValues); } //set listValues to list passed as parm //use this method to "reset" our ArrayList data before each test public void setArrayList(ArrayList<Integer> newValues) { listValues = new ArrayList<Integer>(newValues); } //method to print the current values in the ArrayList //or you can simply use println() with the array list as the //parameter public void printArrayList() { System.out.println(listValues); } public void swapFirstAndLast() { //save first element int temp = listValues.get(0); //move last element to first position (0) listValues.set(0, listValues.get(listValues.size()-1)); //put temp value into last position (size() -1) listValues.set(listValues.size()-1, temp); } //part b public void shiftRight() { } //part c, set all even elements to 0. public void setEvensToZero() { } //part d, replace each element except the first and last by larger of two //around it public void largerOfAdjacents() { } //part e, remove middle el if odd length, else remove middle two els. public void removeMiddle() { }
Hello I am a beginner in Java and am having issues with ArrayLists and formulating methods. I had little issue with formulating methods for Arrays, but ArrayLists seems to be difficult for me for some reason. Here's what needs to be done.
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).
And here is my code so far.
import java.util.Collections; //need this one for sorting ArrayList import java.util.Random; import java.util.ArrayList;
public class ArrayListMethods { //ArrayList to hold values to work on for lab private ArrayList<Integer> listValues;
//ArrayList constructor public ArrayListMethods(ArrayList<Integer> initialValues) { //note this shows how to make a copy of an array list //simply pass the old list in as an argument to //the ArrayList constructor listValues = new ArrayList<Integer>(initialValues); } //set listValues to list passed as parm //use this method to "reset" our ArrayList data before each test public void setArrayList(ArrayList<Integer> newValues) { listValues = new ArrayList<Integer>(newValues); } //method to print the current values in the ArrayList //or you can simply use println() with the array list as the //parameter public void printArrayList() { System.out.println(listValues); } public void swapFirstAndLast() { //save first element int temp = listValues.get(0); //move last element to first position (0) listValues.set(0, listValues.get(listValues.size()-1)); //put temp value into last position (size() -1) listValues.set(listValues.size()-1, temp); } //part b public void shiftRight() { } //part c, set all even elements to 0. public void setEvensToZero() { } //part d, replace each element except the first and last by larger of two //around it public void largerOfAdjacents() { } //part e, remove middle el if odd length, else remove middle two els. public void removeMiddle() { }
Explanation / Answer
//Please visit http://pastebin.com/fVEgZDFu for well indented version of the code
import java.util.Collections; //need this one for sorting ArrayList
import java.util.Random;
import java.util.ArrayList;
public class ArrayListMethods {
//ArrayList to hold values to work on for lab
private ArrayList<Integer> listValues;
//ArrayList constructor
public ArrayListMethods(ArrayList<Integer> initialValues) {
//note this shows how to make a copy of an array list
//simply pass the old list in as an argument to
//the ArrayList constructor
listValues = new ArrayList<Integer>(initialValues);
}
//set listValues to list passed as parm
//use this method to "reset" our ArrayList data before each test
public void setArrayList(ArrayList<Integer> newValues) {
listValues = new ArrayList<Integer>(newValues);
}
//method to print the current values in the ArrayList
//or you can simply use println() with the array list as the
//parameter
public void printArrayList() {
System.out.println(listValues);
}
public void swapFirstAndLast() {
//save first element
int temp = listValues.get(0);
//move last element to first position (0)
listValues.set(0, listValues.get(listValues.size()-1));
//put temp value into last position (size() -1)
listValues.set(listValues.size()-1, temp);
}
//part b
public void shiftRight() {
//save last element
int temp = listValues.get(listValues.size()-1);
//shift all elements to the right by 1except last element
for(int i=listValues.size()-1;i>0;i--){
listValues.set(i, listValues.get(i-1));
}
//shift the last element to the first
listValues.set(0, temp);
}
//part c, set all even elements to 0.
public void setEvensToZero() {
//Start with second element and move in steps of 2
for(int i=1;i<listValues.size();i+=2){
listValues.set(i, 0);
}
}
//part d, replace each element except the first and last by larger of two
//around it
public void largerOfAdjacents() {
//loop variable and previous element
int prev,i;
//initialize the previous element as 0th element
for(i=1,prev=listValues.get(0);i<listValues.size()-1;i++){
//if previous element is greater than following element
if(prev < listValues.get(i+1)){
//update the previous as current element
prev = listValues.get(i);
//set the current element as following element
listValues.set(i,listValues.get(i+1));
}
else{
////update the previous as current element
prev = listValues.get(i);
////set the current element as previous element
listValues.set(i,prev);
}
}
}
//part e, remove middle el if odd length, else remove middle two els.
public void removeMiddle() {
//If length is odd
if(listValues.size()%2 == 1){
//remove single middle element
listValues.remove(listValues.size()/2);
}
else{
//Remove the second middle element
listValues.remove(listValues.size()/2);
//Remove the only left middle element
listValues.remove(listValues.size()/2);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.