Closed Thread
Page 1 of 3 123 LastLast
Results 1 to 10 of 26

Thread: How to replace an existing data in an array

  1. #1
    Xdawn90 is offline Learning Programmer
    Join Date
    Jul 2009
    Location
    in a fantasy world ...
    Posts
    54
    Rep Power
    0

    How to replace an existing data in an array

    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. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,525
    Blog Entries
    75
    Rep Power
    144

    Re: How to replace an existing data in an array

    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Sinipull's Avatar
    Sinipull is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Tallinn, Estonia, Estonia
    Posts
    383
    Rep Power
    13

    Re: How to replace an existing data in an array

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

  5. #4
    Xdawn90 is offline Learning Programmer
    Join Date
    Jul 2009
    Location
    in a fantasy world ...
    Posts
    54
    Rep Power
    0

    Re: How to replace an existing data in an array

    I have a question here:

    why am I getting a nullpointerexception in this code ?

    Code:
    public class Showroom{
    
       private Vehicle stocks[];   
       private double width;
       private double height;
       private double length;
    
    //--------------------------------
       
       public Showroom(){
    	  stocks[0]=new Vehicle();
    	  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[]){
          Showroom s = new Showroom();
    	  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.

  6. #5
    Sinipull's Avatar
    Sinipull is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Tallinn, Estonia, Estonia
    Posts
    383
    Rep Power
    13

    Re: How to replace an existing data in an array

    instead of

    Code:
    private Vehicle stocks[];
    try that...

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

  7. #6
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,849
    Blog Entries
    4
    Rep Power
    49

    Re: How to replace an existing data in an array

    Your stocks and vehicle, are they separate classes or what?

  8. #7
    Sinipull's Avatar
    Sinipull is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Tallinn, Estonia, Estonia
    Posts
    383
    Rep Power
    13

    Re: How to replace an existing data in an array

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

  9. #8
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,849
    Blog Entries
    4
    Rep Power
    49

    Re: How to replace an existing data in an array

    Quote Originally Posted by Sinipull View Post
    i think "stocks" is just the name of array..
    If thats so, then where is the vehicle class?

  10. #9
    Sinipull's Avatar
    Sinipull is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Tallinn, Estonia, Estonia
    Posts
    383
    Rep Power
    13

    Re: How to replace an existing data in an array

    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.

  11. #10
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,849
    Blog Entries
    4
    Rep Power
    49

    Re: How to replace an existing data in an array

    Quote Originally Posted by Sinipull View Post
    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

Closed Thread
Page 1 of 3 123 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. use of array to read student data
    By jackson6612 in forum C and C++
    Replies: 15
    Last Post: 06-08-2011, 07:57 AM
  2. php!? How to get data from DB into ASSOC ARRAY?
    By Stasonix in forum PHP Development
    Replies: 1
    Last Post: 03-24-2011, 07:09 AM
  3. Replies: 3
    Last Post: 08-21-2010, 02:59 AM
  4. C++ Reading Data file into Struct Array HELP!!!
    By kevinsabres in forum C and C++
    Replies: 6
    Last Post: 04-21-2010, 11:48 AM
  5. Can you use vblookup to replace data in Excel VB
    By stackemevs in forum Visual Basic Programming
    Replies: 2
    Last Post: 01-17-2010, 06:21 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts