Question: Using my code import java.util. * ; / / Generic class to store data of any type class GenericArrayStorage { private T [ ] array;
Using my code import java.util.;
Generic class to store data of any type
class GenericArrayStorage
private T array;
private int capacity;
private int size;
Constructor
public GenericArrayStorageint capacity
this.capacity capacity;
this.size ;
this.array T new Objectcapacity;
Method to add data to the array
public void addT data
if size capacity
arraysize data;
else
System.out.printlnArray is full. Cannot add more elements.";
Method to get data from the array at index
public T getint index
if index size && index
return arrayindex;
else
throw new ArrayIndexOutOfBoundsExceptionIndex out of bounds.";
Method to sort the array based on a comparator
public void sortComparator comparator
Arrays.sortarray size, comparator;
Method to display the array
public void display
for int i ; i size; i
System.out.printlnarrayi;
public class Main
public static void mainString args
Scanner scanner new ScannerSystemin;
Creating a generic array storage for Strings
GenericArrayStorage storage new GenericArrayStorage;
Reading data from the user and storing it
for int i ; i ; i
System.out.printEnter data: ;
String data scanner.nextLine;
storage.adddata;
Displaying unsorted data
System.out.printlnUnsorted Data:";
storage.display;
Sorting based on user input String length in this example
System.out.printlnSorting based on length:";
storage.sortComparatorcomparingIntString::length;
Displaying sorted data
storage.display;
scanner.close;
Add Efficiently search the data based on the field specified by the user using a comparator without changing the code
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
