Java

Moderators: zibadian
Number of threads: 7818
Number of posts: 18218

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
problem with project Posted by Pierced1 on 17 Feb 2009 at 5:24 PM
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 save and return the values either.

only 2 classes are involved in this process. Big thanks for any help.
//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;
}

}

****************************************************************

//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.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.