Jump to content

How to replace an existing data in an array

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
25 replies to this topic

#1
Xdawn90

Xdawn90

    Learning Programmer

  • Members
  • PipPipPip
  • 55 posts
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.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Arrays
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
also, i would recommend you learn about ArrayList and LinkedList, which are much easier to use than regular arrays.

#4
Xdawn90

Xdawn90

    Learning Programmer

  • Members
  • PipPipPip
  • 55 posts
I have a question here:

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
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
instead of

private Vehicle stocks[];  

try that...

private Vehicle stocks[] = new Vehicle[sizeOfArray]; 


#6
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts
Your stocks and vehicle, are they separate classes or what?
Posted Image

#7
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
i think "stocks" is just the name of array..

#8
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

Sinipull said:

i think "stocks" is just the name of array..

If thats so, then where is the vehicle class?
Posted Image

#9
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
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.

#10
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

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.

Good :)
Posted Image

#11
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts

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
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts

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 :>
Posted Image