Closed Thread
Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 26

Thread: How to replace an existing data in an array

  1. #11
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: How to replace an existing data in an array

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

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #12
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Re: How to replace an existing data in an array

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

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

    Re: How to replace an existing data in an array

    Sure, but they are still much more comfortable to use and should be learned anyway. And i don't see big problem using them during "learning period" while systems aren't huge and companies don't depend on them...

  5. #14
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: How to replace an existing data in an array

    Quote Originally Posted by Turk4n View Post
    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 :>
    Storing large amounts of data in these structures is nothing, the problem with them is frequent insertion and deletion. Arrays are not always faster, because when you insert a new item into the array you do not have to resize the array, where as with the vector you do. However, if you need the size of the array to grow, you would have to manually resize it, and copy the old content. This is slow, and essentially is what the vector does. Thus if you are inserting and deleting often, you are resizing and moving items to fill gaps often, which is very inefficient.


    I agree you should learn to use them, but you should learn when to use them as well.
    Last edited by chili5; 07-05-2009 at 04:28 AM.

  6. #15
    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

    Thanks for the replies.

    I have encounter a problem now hope that you guys can help me.

    I cant figure out what is the problem again. >< Here is my code.

    Code:
    public class Game{
    
       private Person players[];
    
    //---------------------------------
    
    public Game(){
          players=new Person[3];
          players[0]=new Person();
          players[1]=new Person();
          players[2]=new Person();
       }
       public void setPlayers(Person p, int a){
          players[a]=p;
       }
       public Person getPlayers(int a){
          return players[a];
       }
       public static void main(String args[]){
          Game g = new Game();
    	  Person p = g.setPlayers(p, 0);
    	  p.setName("kl");
    	  p.setHp(9);
    	  p.setSp(8);
    	  p.setExp(0.5);
       }
    }
    Code:
    public class Person{
    
       private String name;
       private int hp;
       private int sp;
       private double exp;
       private Jobskill skills[];
       private Equipment defense[];
       private Equipment attack[];
    
    //---------------------------------------------
    
       public Person(){
          name="";
          hp=10;
          sp=10;
          exp=0.0;
          skills=new Jobskill[5];
          skills[0]=new Jobskill();
          skills[1]=new Jobskill();
          skills[2]=new Jobskill();
          skills[3]=new Jobskill();
          skills[4]=new Jobskill();
          defense=new Equipment[5];
          defense[0]=new Equipment();
          defense[1]=new Equipment();
          defense[2]=new Equipment();
          defense[3]=new Equipment();
          defense[4]=new Equipment();
          attack=new Equipment[5];
          attack[0]=new Equipment();
          attack[1]=new Equipment();
          attack[2]=new Equipment();
          attack[3]=new Equipment();
          attack[4]=new Equipment();
       }
    
       public void setName(String a){
          name=a;
       }
       public String getName(){
          return name;
       }
       public void setHp(int b){
          hp=b;
       }
       public int getHp(){
          return hp;
       }
       public void setSp(int c){
          sp=c;
       }
       public int getSp(){
          return sp;
       }
       public void setExp(double e){
          exp=e;
       }
       public double getExp(){
          return exp;
       }
       public void setSkills(Jobskill j, int x){
          skills[x]=j;
       }
       public Jobskill getSkills(int x){
          return skills[x];
       }
       public void setDefense(Equipment e, int y){
          defense[y]=e;
       }
       public Equipment getDefense(int y){
          return defense[y];
       }
       public void setAttack(Equipment e, int z){
          attack[z]=e;
       }
       public Equipment getAttack(int z){
          return attack[z];
       }
    }
    Thanks very much.

  7. #16
    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

    Thanks for the replies.

    I have encounter a problem now and hope you guys can help me out. I keep getting an error saying "incompatible types. Found: void. Required: Person.". Here is my code...

    Code:
    public class Person{
    
       private String name;
       private int hp;
       private int sp;
       private double exp;
       private Jobskill skills[];
       private Equipment defense[];
       private Equipment attack[];
    
    //---------------------------------------------
    
       public Person(){
          name="";
          hp=10;
          sp=10;
          exp=0.0;
          skills=new Jobskill[5];
          skills[0]=new Jobskill();
          skills[1]=new Jobskill();
          skills[2]=new Jobskill();
          skills[3]=new Jobskill();
          skills[4]=new Jobskill();
          defense=new Equipment[5];
          defense[0]=new Equipment();
          defense[1]=new Equipment();
          defense[2]=new Equipment();
          defense[3]=new Equipment();
          defense[4]=new Equipment();
          attack=new Equipment[5];
          attack[0]=new Equipment();
          attack[1]=new Equipment();
          attack[2]=new Equipment();
          attack[3]=new Equipment();
          attack[4]=new Equipment();
       }
    
       public void setName(String a){
          name=a;
       }
       public String getName(){
          return name;
       }
       public void setHp(int b){
          hp=b;
       }
       public int getHp(){
          return hp;
       }
       public void setSp(int c){
          sp=c;
       }
       public int getSp(){
          return sp;
       }
       public void setExp(double e){
          exp=e;
       }
       public double getExp(){
          return exp;
       }
       public void setSkills(Jobskill j, int x){
          skills[x]=j;
       }
       public Jobskill getSkills(int x){
          return skills[x];
       }
       public void setDefense(Equipment e, int y){
          defense[y]=e;
       }
       public Equipment getDefense(int y){
          return defense[y];
       }
       public void setAttack(Equipment e, int z){
          attack[z]=e;
       }
       public Equipment getAttack(int z){
          return attack[z];
       }
    }
    Code:
    public class Game{
    
       private Person players[];
    
    //---------------------------------
    
       public Game(){
          players=new Person[3];
          players[0]=new Person();
          players[1]=new Person();
          players[2]=new Person();
       }
       public void setPlayers(Person p, int a){
          players[a]=p;
       }
       public Person getPlayers(int a){
          return players[a];
       }
       public static void main(String args[]){
          Game g = new Game();
    	  Person p = g.setPlayers(p, 0);
    	  p.setName("kl");
    	  p.setHp(9);
    	  p.setSp(8);
    	  p.setExp(0.5);
       }
    }
    Thanks very much.

  8. #17
    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

    Sorry for my clumsy mistake of posting two similar posts... This is due to my Internet connection.

    I apologize for that. Sorry.

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

    Re: How to replace an existing data in an array

    Code:
    Person p = g.setPlayers(p, 0);
    Well, setPlayers returns void, so why would you want to assign it to Person p?

    try this.
    Code:
    g.setPlayers(p, 0);
    i'm just guessing now.
    next time, please describe the problem too.. .

  10. #19
    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

    Sorry for my unclear description.

    Player is an array of Person. I want to set new values to the properties of the person p. I know maybe my method of doing it is wrong but i just cant figure out how to do that.

    How to set new value of an object's properties where the object is stored inside an array?

    Facing headache now. ><

    Please correct me if i use the wrong term. Thanks.

  11. #20
    Sinipull's Avatar
    Sinipull is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Tallinn, Estonia, Estonia
    Posts
    382
    Rep Power
    13

    Re: How to replace an existing data in an array

    How to set new value of an object's properties where the object is stored inside an array?
    players[0].yourMethod();

    it's simple as that..

Closed Thread
Page 2 of 3 FirstFirst 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