Right now i have an array of objects. Each object contain code, name, price and amount.
1. How am I going to add an object containing all those properties into the array ?
2. How am I going to edit(replace with new properties) the properties of the object inside the array ?
Please help.
Thanks very much.
How to replace an existing data in an array
Started by Xdawn90, Jul 04 2009 04:25 AM
25 replies to this topic
#1
Posted 04 July 2009 - 04:25 AM
|
|
|
#2
Posted 04 July 2009 - 05:10 AM
#3
Posted 04 July 2009 - 06:05 AM
also, i would recommend you learn about ArrayList and LinkedList, which are much easier to use than regular arrays.
#4
Posted 04 July 2009 - 07:08 AM
I have a question here:
why am I getting a nullpointerexception in this code ?
The one in red is the one that the compiler complaining about. Can you tell me what is wrong with that ? I really cant figure out what is wrong.
Thanks.
why am I getting a nullpointerexception in this code ?
public class Showroom{
private Vehicle stocks[];
private double width;
private double height;
private double length;
//--------------------------------
public Showroom(){
[COLOR="Red"]stocks[0]=new Vehicle();[/COLOR]
stocks[1]=new Vehicle();
stocks[2]=new Vehicle();
stocks[3]=new Vehicle();
stocks[4]=new Vehicle();
width=0.0;
height=0.0;
length=0.0;
}
public void setStocks(Vehicle v, int x){
stocks[x]=v;
}
public Vehicle getStocks(int x){
return stocks[x];
}
public double calculateArea(){
return width*height*length;
}
public void setWidth(double a){
width=a;
}
public double getWidth(){
return width;
}
public void setHeight(double b){
height=b;
}
public double getHeight(){
return height;
}
public void setLength(double c){
length=c;
}
public double getLength(){
return length;
}
public static void main(String args[]){
[COLOR="Red"]Showroom s = new Showroom();[/COLOR]
Vehicle myVehicle;
myVehicle = new Vehicle("abc", "green", 3, 3, 3);
s.setStocks(myVehicle, 0);
System.out.println(s.getStocks(0));
}
}
The one in red is the one that the compiler complaining about. Can you tell me what is wrong with that ? I really cant figure out what is wrong.
Thanks.
#5
Posted 04 July 2009 - 07:29 AM
instead of
try that...
private Vehicle stocks[];
try that...
private Vehicle stocks[] = new Vehicle[sizeOfArray];
#6
Posted 04 July 2009 - 09:10 AM
#7
Posted 04 July 2009 - 10:07 AM
i think "stocks" is just the name of array..
#8
Posted 04 July 2009 - 10:14 AM
Sinipull said:
i think "stocks" is just the name of array..
If thats so, then where is the vehicle class?
#9
Posted 04 July 2009 - 10:53 AM
Guess he didn't show us that, it doesn't matter anyway...
i already posted the solution and i'm quite sure, it will fix it.
i already posted the solution and i'm quite sure, it will fix it.
#10
Posted 04 July 2009 - 10:59 AM
Sinipull said:
Guess he didn't show us that, it doesn't matter anyway...
i already posted the solution and i'm quite sure, it will fix it.
i already posted the solution and i'm quite sure, it will fix it.
Good :)
#11
Posted 04 July 2009 - 01:11 PM
Sinipull said:
also, i would recommend you learn about ArrayList and LinkedList, which are much easier to use than regular arrays.
Not necessarily. What if you do not need vector like operations for your program?
#12
Posted 04 July 2009 - 09:48 PM
chili5 said:
Not necessarily. What if you do not need vector like operations for your program?
Indeed, learning Lists and vectors are not 100% needed or useful !
When dealing with larger data and larger scale of applications with more information to store, sort, delete and etc. Lists and vectors will reduce the speed response !
Arrays always faster than Lists/vectors in Java :>


Sign In
Create Account


Back to top









