I'm working on an array of objects in java.
I've done an Add, it works. and I'm having problems in implementing a Delete (from the array).
objects are from class called Student, it has a name and ID as members
my work:
// delete an object
if (count == 0)
System.out.println("Sorry there are no items in the system");
else {
System.out.print("Please enter the ID of Student you'd like to Delete: ");
String searchID = in.nextLine();
for (int i =1 ; i<count; i++) { // first : search for the object
if (searchID.equalsIgnoreCase(Students[i].getID())) {
System.out.print("Are you sure you want do delete "
+ Students[i].getName()+ " from the System? ");
String ans = in.nextLine();
if (ans.equalsIgnoreCase("no")) { break; }
if (ans.equalsIgnoreCase("yes")) {
Students[i] = Students[Students.length-1];
break;
}
} else {
System.out.println("Sorry, you need to type a valid ID to delete it's object.. ");
}
}
Before your for
loop create a new Array with the same size as the input array.
Only add to the new Array if you do not want to delete it.
Afterwards, ignore array items with null value
You cannot easily achieve this in array. You can try arraylist.
arrayList.remove(Object) or arrayList.remove(index)
If you want to do it array then.
First, you should probably just use an ArrayList and use a built-in as discussed here . If you're bent on using an array, you need to resize the array after copying the last element of Students to the deleted object. Without this truncation, you're just duplicating the last element of your array.
Because you can't resize java arrays, this really means you need to copy the all of the array except the last element to a new array. For this, you can try adding
Students = Arrays.copyOf(Students, Students.length-1);
before your break. But you have a lot less memory overhead if you use ArrayList.
First of all I couldn't understand the logic of your code. You should check the answer before getting 100*2 input from the user. Anyway, if you really want to do this with simple java array, you should do something like this, and then remove null objects. This is an inefficient method:
String[] a = {"student1", "student2"};
String[] b = {"strudnt3"};
String[] c = new String[a.length + b.length];
// removing one object
a[1] = null;
// copying both arrays to c
System.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(b, 0, c, a.length, b.length);
// ignore null objects
System.out.println(Arrays.toString(c));
The better method is using ArrayList
: You can easily add and remove objects from a List. Take a look at the JavaDoc, here http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
If you want to rewrite the code with the ArrayList:
// creat an ArrayList of the object
ArrayList<Student> studentList = new ArrayList<Student>();
// then, ask if the user wants to add object
Scanner in = new Scanner (System.in);
System.out.print("Would you like to add an object?");
while (true) {
String ans = in.nextLine();
if (ans.equals("no"))
{
break;
}
// else, create the object and add it to the ArrayList
// you can add the attributes from the constructor
studentList.add(new Student(22, "name"));
}
Then you can easily make an Iterator
, doing a Loop inside your List and match any field you want, and finally remove the object from the list:
if (//find the object) {
studentList.remove(object)
}
check also this Question for removing the object from the ArrayList: remove an object from a List
As other have suggested the best datastructure is to use ArrayList, as it gives inbuilt api methods to dynamically resize Array efficiently. It relives the developer from rewriting it again.
If at all for learning, from above example. you can assign null at removed index, OR best way
you can achieve all this just by using Arraylist, why not use it ;)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.