Results 1 to 2 of 2
  1. #1
    Pierced1 is offline Member
    Join Date
    Feb 2009
    Posts
    12
    Rep Power
    0

    Post [SOLVED] Problem with program(getters/setters,arrayList).

    I'm in the middle of my largest project so far and really could use some help, I have an array of strings that i want to convert to an array of objects that are referenced (i think it's the right word) to other parts of the array.

    Like a child has parents. index 1 is referenced to 3 and 4. Where 1=child and 3+4=it's parents.

    String array [ String1,String2,String3....]
    into
    Object array [program.Cat@5dcec6, program.Cat@b25b9d....]

    so it seems like it does create an array with objects but i'm missing something, i cant get what the object contains..i think it's something wrong with the constructor in Cat. So the object contains 3 int and one String atm. I'm not sure i have done the constructor correctly to get and set the values either.

    only 2 classes are involved in this process. Big thanks for any help.

    Java Code:
    //the constructor
    package program;
    
    import java.util.*;
    
    /**
     *
     * @author Stormy
     */
    
    public class Cat {
    
    //this is the new arrays containing the objects.
    public static ArrayList<Cat> sireCats = new ArrayList<Cat>();
    public static ArrayList<Cat> damCats = new ArrayList<Cat>();
    
    private String ems;
    private int partner;
    private int father;
    private int mother;
    
    public Cat(){
    }
    
    public void setEms(String e){
        this.ems=e;
    }
          
    public String getEms(){
        return ems;
    }
    
    public void setFather(int c){
        this.father = c;
    }
    
    public int getFather(){
        return father;
    }
    
    public void setMother(int c){
        this.mother = c;
    }
    
    public int getMother(){
        return mother;
    }
    
    public void setPartner (int c){
        this.partner = c;
    }
    
    public int getPartner (){
        return partner;
    }
    
    }
    ************************************

    Java Code:
    //the converter from String array to object array
    
    package program;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     *
     * @author Stormy
     */
    public class ObjectifyArray {
      //String array = list.
       public void ArrayToObjects(List<String> list,int sex){//gets list and sex of cat
        for(int i = 0; i < list.size(); i++ ){
            int mother = i*2;     //sets mothers index
            int father = (i*2)+1; //sets fathers index
            int partner = i+1;  //sets partners index 
            Cat catToAdd = new Cat();
            if(sex==1){
            catToAdd.setEms(list.get(i));
            catToAdd.setMother(mother);
            catToAdd.setFather(father);
            catToAdd.setPartner(partner);
            Cat.sireCats.add(catToAdd);
            }
            else if(sex==2){
            catToAdd.setEms(list.get(i));
            catToAdd.setMother(mother);
            catToAdd.setFather(father);
            catToAdd.setPartner(partner);
            Cat.damCats.add(catToAdd);
            }
            }
            System.out.println("damCats "+Cat.damCats);
            System.out.println("sireCats "+Cat.sireCats);
            
        }
    }
    if there are other suggestions on how to do this the best way they are more then welcome.

  2. #2
    JohnnyR is offline Member
    Join Date
    Feb 2009
    Posts
    46
    Rep Power
    0

    Default

    You'll need a 'Object' class or Parent class.
    In that have it static reference to the String array and positions with in it - then make your new object array and input the new objects created.

Similar Threads

  1. Hey! ArrayList problem here
    By Samgetsmoney in forum New To Java
    Replies: 31
    Last Post: 02-20-2009, 01:39 AM
  2. Getters and Setters
    By Charliestons in forum New To Java
    Replies: 10
    Last Post: 09-12-2008, 11:57 PM
  3. ArrayList problem with images
    By Cymro in forum New To Java
    Replies: 2
    Last Post: 02-05-2008, 07:22 PM
  4. ArrayList problem
    By khamuruddeen in forum New To Java
    Replies: 7
    Last Post: 12-22-2007, 06:46 AM
  5. How To Add Fields And Generating Getter-Setters
    By JavaForums in forum NetBeans
    Replies: 0
    Last Post: 07-31-2007, 12:13 AM

Posting Permissions

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