Reputation: 15034
How can i achieve this in java. i have an object which has properties
.
public class Object {
private final Credentials Credentials;
private final int PageSize;
private final int PageStart;
private final int DefaultFilterId;
public Object(Credentials Credentials, int PageSize, int PageStart,
int DefaultFilterId) {
this.Credentials = Credentials;
this.PageSize = PageSize;
this.PageStart = PageStart;
this.DefaultFilterId = DefaultFilterId;
}
}
Now i am forming a this object like this
Object obj = new Object(args);
At some point i need the same Object, with new properties
added but removing some
.
I do something like this in javascript.
var myCars=new Array();
myCars[0]="Saab";
myCars[1]="Volvo";
myCars[2]="BMW";
delete myCars[1]; or myCars.splice(1,1);
Upvotes: 4
Views: 25411
Reputation: 22822
public class Object {
private Credentials credentials;
private int PageSize;
private int PageStart;
private int DefaultFilterId;
public Object(Credentials credentials, int PageSize, int PageStart, int DefaultFilterId) {
this.credentials = credentials;
this.PageSize = PageSize;
this.PageStart = PageStart;
this.DefaultFilterId = DefaultFilterId;
}
// do that for the properties you want to be able to modify
public void setCredentials(Credentials newCredentials) {
this.credentials = newCredentials;
}
}
And you use that:
object.setCredentials(yourNewCredentials)
Also, you shouldn't name your object "Object", it's the base class for all the classes in Java.
Upvotes: 1
Reputation: 46408
put all your instances of your object in a collection, and then you can delete it from the collection.
List<YourObject> list = new ArrayList<YourObject>();
YourObject obj1 = new YourObject("abc");
list.add(obj1);
YourObject obj2 = new YourObject("xyz");
list.add(obj2);
now both your objects are inside a list . later you can use the remove method an remove them.
list.remove(obj1);
and just a pointer, its a bad practice to name your class as Object
as all java classes extend
from java.lang.Object
.
Upvotes: 3
Reputation: 1650
You could add an ArrayList as private property to your class. And than build access-functions that allow you to add and delete entries. You can't do it exactly like in JavaScript.
Upvotes: 1
Reputation: 1055
You can´t do that in java. The best approximation is to use HashTable or similar.
Hashtable ht = new Hashtable();
ht.put("key", value);
Upvotes: 1