In Java, for each line create a method that -Removes ith element from list and r
ID: 3750246 • Letter: I
Question
In Java, for each line create a method that
-Removes ith element from list and returns it
delete(1) [1,2,3] would delete 2 leaving [1,3] and return 2
-Add an int to the end of a list
add(1) [1,2,3] would leave [1,2,3,1] etc.
-return the ith element
fetch(1) [1,2,3] would return 2 etc.
-set the ith element with int and return it
set(1,3) given [1,2,3] would change to [1,3,3] and return 2
-move the ith element so that it becomes the jth element
mutate(1,3) would change [1,2,3,4] to [1,3,4,2] etc.
-return an array containing the elements of the list in order
convertArray() turns [1,2,3] into {1,2,3}
-Return current number of elements in the list
Explanation / Answer
Below code snipped would perform the following operations on list of integers:
1. Declare a List of integers
2. Delete ith element of list
3. Add elment to end of list
4. return ith element of the list
5. set the ith element with int and return it
6.move the ith element so that it becomes the jth element
7.return an array containing the elements of the list in order
8.Return current number of elements in the list
For each of the operation , a method has been declared and the same has been called in the main method.
========== Code =============================
import java.io.*;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
public class IntegerOperations{
//declare list of integers as global variable so that all methods can access
public static List<Integer> list = new ArrayList<Integer>();
//declare array if integers
public static Integer[] arrList ;
public static void main(String []args){
int i =1;
list.add(1);
list.add(2);
list.add(3);
System.out.println("Integer list : "+ list);
System.out.println("Delete ith element from the list: ");
// method call to Delete the ith element from the list and display the ith element
int d = delete(i);
System.out.println(d);
System.out.println("Add an int to end of the list: ");
// method call to add int to the the list and display the elements
add(1);
System.out.println(list);
System.out.println("Return ith element of the list: ");
// method call to return ith element of the the list and display the element
int r = fetch(i);
System.out.println(r);
System.out.println("Set the ith element to the list: ");
// method call to set ith element of the the list and display the element
int setVal = set(i,3);
System.out.println(setVal);
System.out.println(list); //print the list
System.out.println("Move the ith element of the list to jth position: ");
// method call to move ith element of the the list and display the list
mutate(1,3);
System.out.println(list);
System.out.println("Return an array containing the elements of the list in order : ");
// method call to return an array with ith elements of the the list and display the elements
Integer[] aList = convertArray();
System.out.println(Arrays.toString(aList));
System.out.println("Return the current number of elements in the list : ");
// method call to return current number of elements in the list
System.out.println(countElements());
}
// method call to Delete the ith element from the list
public static int delete(int i)
{
int returnVal = list.get(i);
list.remove((int) i);
return returnVal;
}
// method to add int to the the list
public static void add(int a)
{
list.add(a);
}
// method to return ith element of the the list
public static int fetch(int i)
{
return list.get(i);
}
// method to set ith element of the the list
public static int set(int i,int value)
{
int returnVal = list.get(i);
list.set(i,value);
return returnVal;
}
// method to move ith element of the the list
public static void mutate(int i, int j)
{
int val = list.get(i);
list.remove((int) i);
list.add(val);
if(j < list.size())
list.set(j,val);
}
// method to return an array with ith elements of the the list
public static Integer[] convertArray()
{
arrList = new Integer[list.size()];
for(int i=0;i<list.size();i++)
{
arrList[i] = list.get(i);
}
return arrList;
}
// method to return current number of elements in the list
public static int countElements()
{
return list.size();
}
}
=================================End================================
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.