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.