#20 Use any of the following JAVA ListInterface methods to answer the question b
ID: 671916 • Letter: #
Question
#20
Use any of the following JAVA ListInterface methods to answer the question below. Note that toArray is not listed and should not be used in your solution.
public boolean add(T newEntry)
public boolean add(int newPosition, T newEntry)
public boolean isEmpty()
public boolean contains(T anObject)
public boolean remove(int givenPosition)
public boolean replace(int givenPosition, T newEntry)
public T getEntry(int givenPosition)
public int getLength()
public void clear()
Write a complete JAVA method at the client level to remove entries between two indices of a list. The method header is:
public void removeBetween(ListInterface<T> list, int start, int end)
The method should remove the elements between the beginning and ending index (inclusive- meaning also remove the element at start and the element at end). Your method must handle if either index is not a valid list position- the method cannot crash, but you can choose what it should do instead.
For this question, you are writing code at the client level. This means you do not know how the list is implemented.
Explanation / Answer
import java.util.*;
import java.util.List;
public class Programming1 {
/* logic of this method is as follows:
* if you remove one element is the list then all the following elements will move
* one position forward. example suppose list has 4 elements.
* 10,20,30,40. If you remove 20 from list then list will become 10,30,40.
*
*So simple you can't remove element from list by simply using for loop in which
*you start iterating from start value and stop at end value and call method
*list.remove(i);
*
*So what I did is First I calculate the no of elements you want to delete.
*for example list contains 10,20,30,40,50 and you want to remove from 1 to 3.
*then no elements you want to remove is element at index 1, at index 2, at index 3
* means 3 elements which is "end_index-start_index +1;"
*
* Now if you remove element at index 1 then list will become 10,30,40,50. element at
* index 2 will move to index 1 and element at index 3 will move to index 2 and so on.
*
* now you want to remove element at index 2 but index 2 element comes to index 1. So you need to
* remove again element at index 1. Now again what will happen all the elements move one position backward.
*
* Again element at index 3 comes to index 1. So you need to again remove element at index 1.
*
* You have to do this same thing by no elements you want to remove which we calculated above(end_index-start_index +1).
*
*
* This is the whole logic !!
* */
void removeBetween(List list, int start, int end)
{
int length= end-start+1; // calculating no of elements you want to remove
for(int i=0;i<length;i++)
{
list.remove(start); // removing elements at index start , length no of times.
}
}
public static void main(String[] args)
{
/* Declaration of variable for start and end index */
int start,end;
/*As mentioned we are writing for client side and we dont know how that list is implemented.
*For this purpose I am using below Syntax for defining List.
*
*Here list is a reference variable of type List which is a interface. you know that ArrayList and LinkedList
*implement List interface. So by doing this I am making this declaration Flexible that in future if you want list to be implemented as LinkedList then
*you need to change only one line--> List<Integer> list=new LinkedList<Integer>();
*
*Please note that by doing this you can only use those methods which is defined in List Interface not ArrayList or Linkedlist methods.
*
*
* */
List<Integer> list=new ArrayList<Integer>();
/* Scanner object to read inputs from user */
Scanner s=new Scanner(System.in);
System.out.println("Enter no of elements you want to enter");
int n=s.nextInt();
System.out.println("input elements");
/* Adding of element in the list */
for(int i=0;i<n;i++)
{
list.add(s.nextInt());
}
System.out.println("enter start and End index");
start=s.nextInt();
end=s.nextInt();
/*Logic to check whether entered range is valid or not.
* If end index is less than start index or end index is outside the list. Then range is invalid. */
if(end <start || end > n)
{
System.out.println("Invalid Range");
System.exit(0);
}
Programming1 p= new Programming1();
//calling of method
p.removeBetween(list, start,end);
System.out.println("Final List is");
for(int j: list)
{
System.out.println(j);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.