S
Sanny
I have a LinkedList of Object.
I find it very inefficient to add or retrive object from Linked List.
I have a Object Car as below and Linked list carlist
--------------------->>>>>> Class Car Start
class car extends Object{
private int points;
private String carname;
public car(int points111,int carname111){
points=points111;
carname=carname111;
}
public int getpoints(){
return(points);
}
public String getname(){
return(carname);
}
public void setname(int carname111){
carname=carname111;
}
public void setpoint(int carpoint111){
points=carpoint111;
}
}
--------------------->>>>>> Class Car End
Now I have Linked list carlist
LinkedList carlist = new LinkedList();
To add a Car with name "Ferrari", and points 900 to linkerd list what
I do is.
car dummycar = new car("Ferrari", 900) ;
carlist.add(dummycar);
Now I have Two Problems.
First Problem
-------------------
Each time I have to create a new object dummycar in for loop to add to
CarList.
for (int i=1; i<100000;i++){
String carname1=getname(i);
int carpoint1=getpoint(i);
car dummycar = new car(carname1,carpoint1) ;// Takes lot of time to
create this Object ????
carlist.add(dummycar);
}
If I Create dummycar as a Global Variable and use below code
for (int i=1; i<100000;i++){
String carname1=getname(i);
int carpoint1=getpoint(i);
dummycar.setname(carname1);//DummyCar as Global Variable
dummycar.setpoints(carpoint1);
carlist.add(dummycar);
}
Will the above code work without any Problem???
Second Problem
----------------------
Simmilarly Say I have to change Order of Class based on points. Say to
move 1st element to 555th place
Car dummycar=(car)carlist.get(1);
carlist.add(555,dummycar);// move to 555th place.
carlist.remove(1); // removes 1st object.
Here again I have to initialize a dummycar and then add it to 555th
place. It there any way I can directly say to linked list to swap
values from 1st place to 555th place without creating dummycar
variable?
And incase I have to Change the Value of some value in the Linked List
I have to again create a Dummycar
Car dummycar=(car)carlist.get(999);
String name1=dummycar.getname()
Is there a way to not create a seperate Object and use carlist
directly.
Like below
String name1=((car)carlist.get(999)).getname();
Will the above code work???
How can I make swaping Objects in Linked List much faster instead of
creating Dummy Variables and copying values to them, Is there a way to
do that directly?
Bye
Sanny
I find it very inefficient to add or retrive object from Linked List.
I have a Object Car as below and Linked list carlist
--------------------->>>>>> Class Car Start
class car extends Object{
private int points;
private String carname;
public car(int points111,int carname111){
points=points111;
carname=carname111;
}
public int getpoints(){
return(points);
}
public String getname(){
return(carname);
}
public void setname(int carname111){
carname=carname111;
}
public void setpoint(int carpoint111){
points=carpoint111;
}
}
--------------------->>>>>> Class Car End
Now I have Linked list carlist
LinkedList carlist = new LinkedList();
To add a Car with name "Ferrari", and points 900 to linkerd list what
I do is.
car dummycar = new car("Ferrari", 900) ;
carlist.add(dummycar);
Now I have Two Problems.
First Problem
-------------------
Each time I have to create a new object dummycar in for loop to add to
CarList.
for (int i=1; i<100000;i++){
String carname1=getname(i);
int carpoint1=getpoint(i);
car dummycar = new car(carname1,carpoint1) ;// Takes lot of time to
create this Object ????
carlist.add(dummycar);
}
If I Create dummycar as a Global Variable and use below code
for (int i=1; i<100000;i++){
String carname1=getname(i);
int carpoint1=getpoint(i);
dummycar.setname(carname1);//DummyCar as Global Variable
dummycar.setpoints(carpoint1);
carlist.add(dummycar);
}
Will the above code work without any Problem???
Second Problem
----------------------
Simmilarly Say I have to change Order of Class based on points. Say to
move 1st element to 555th place
Car dummycar=(car)carlist.get(1);
carlist.add(555,dummycar);// move to 555th place.
carlist.remove(1); // removes 1st object.
Here again I have to initialize a dummycar and then add it to 555th
place. It there any way I can directly say to linked list to swap
values from 1st place to 555th place without creating dummycar
variable?
And incase I have to Change the Value of some value in the Linked List
I have to again create a Dummycar
Car dummycar=(car)carlist.get(999);
String name1=dummycar.getname()
Is there a way to not create a seperate Object and use carlist
directly.
Like below
String name1=((car)carlist.get(999)).getname();
Will the above code work???
How can I make swaping Objects in Linked List much faster instead of
creating Dummy Variables and copying values to them, Is there a way to
do that directly?
Bye
Sanny