0

incorrect

How to fix this, it says java.lang.UnsupportedOperationException; null(in java.util.AbstractList) on i.remove(); https://code.sololearn.com/cMfULUxMMU8f case 5: //Delete by Name System.out.print("Enter NAME to DELETE: "); name=console.next(); found = false; c= Arrays.asList(p); ListIterator<Person>i = c.listIterator(); while(i.hasNext()){ Person e = i.next(); if (e.getName().equals(name)) { System.out.println("Record Deleted Successfully: "); i.remove(); found = true; } } if (!found){ System.out.println("Record Not Found: "); }

27th May 2022, 12:11 PM
Eyy
3 Answers
+ 1
Don't use a fixed size array, use the Collection API throughout.
27th May 2022, 1:14 PM
Ani Jona šŸ•Š
Ani Jona šŸ•Š - avatar
+ 1
// Hope this link helps you Java List remove() Methods ā€“ ArrayList remove() https://www.journaldev.com/31869/java-list-remove-methods-arraylist-remove
27th May 2022, 1:22 PM
SoloProg
SoloProg - avatar
+ 1
yes, It would be better solution to use Arraylist in whole code. If you don't want to do that, for remove() you can use own method like static void arrayRemove( Object[] array, int index) { /* where copy (shift) all elements from index+1 to index index++ then put one null at the end array[end] = null; */ } in case 5: //i.remove(); arrayRemove(p, i.previousIndex() ); ( i.remove() doesn't work because asList(p) internaly uses fixed size p[] and remove is not supported ) -- another problem in cases 4 and 5 is then possible null in the array in the section at the end of array, so you have to check it before equals() if ( e != null) { and break the loop if e is null
27th May 2022, 8:05 PM
zemiak