+ Reply to Thread
Page 1 of 3
1 2 3 LastLast
Results 1 to 10 of 26

Thread: How to replace an existing data in an array

  1. #1
    Newbie Xdawn90 is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    28

    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. #2
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,668
    Blog Entries
    57

    Re: How to replace an existing data in an array

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

  3. #3
    Programmer Sinipull will become famous soon enough Sinipull's Avatar
    Join Date
    Jun 2009
    Location
    Estonia
    Age
    21
    Posts
    165

    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.

  4. #4
    Newbie Xdawn90 is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    28

    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.

  5. #5
    Programmer Sinipull will become famous soon enough Sinipull's Avatar
    Join Date
    Jun 2009
    Location
    Estonia
    Age
    21
    Posts
    165

    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];

  6. #6
    Code Warrior Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n's Avatar
    Join Date
    May 2008
    Location
    4chan.org/g/
    Age
    20
    Posts
    3,836
    Blog Entries
    4

    Re: How to replace an existing data in an array

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

    Hatsune Miku ~❤❤❤
    初音ミク。~❤❤❤

  7. #7
    Programmer Sinipull will become famous soon enough Sinipull's Avatar
    Join Date
    Jun 2009
    Location
    Estonia
    Age
    21
    Posts
    165

    Re: How to replace an existing data in an array

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

  8. #8
    Code Warrior Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n's Avatar
    Join Date
    May 2008
    Location
    4chan.org/g/
    Age
    20
    Posts
    3,836
    Blog Entries
    4

    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?

    Hatsune Miku ~❤❤❤
    初音ミク。~❤❤❤

  9. #9
    Programmer Sinipull will become famous soon enough Sinipull's Avatar
    Join Date
    Jun 2009
    Location
    Estonia
    Age
    21
    Posts
    165

    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.

  10. #10
    Code Warrior Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n's Avatar
    Join Date
    May 2008
    Location
    4chan.org/g/
    Age
    20
    Posts
    3,836
    Blog Entries
    4

    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

    Hatsune Miku ~❤❤❤
    初音ミク。~❤❤❤

+ Reply to Thread
Page 1 of 3
1 2 3 LastLast

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. C#:Tutorial - Download Data
    By Xav in forum CSharp Tutorials
    Replies: 13
    Last Post: 02-11-2010, 02:17 PM
  2. PHP Arrays
    By chili5 in forum PHP Tutorials
    Replies: 7
    Last Post: 05-22-2009, 04:16 PM
  3. Choosing the right language / data structure / Interface
    By ups in forum General Programming
    Replies: 1
    Last Post: 09-20-2008, 09:11 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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