Results 1 to 2 of 2
- 02-19-2009, 12:01 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 12
- Rep Power
- 0
[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; } }
if there are other suggestions on how to do this the best way they are more then welcome.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); } }
- 02-19-2009, 03:50 AM #2
Member
- Join Date
- Feb 2009
- Posts
- 46
- Rep Power
- 0
Similar Threads
-
Hey! ArrayList problem here
By Samgetsmoney in forum New To JavaReplies: 31Last Post: 02-20-2009, 12:39 AM -
Getters and Setters
By Charliestons in forum New To JavaReplies: 10Last Post: 09-12-2008, 10:57 PM -
ArrayList problem with images
By Cymro in forum New To JavaReplies: 2Last Post: 02-05-2008, 06:22 PM -
ArrayList problem
By khamuruddeen in forum New To JavaReplies: 7Last Post: 12-22-2007, 05:46 AM -
How To Add Fields And Generating Getter-Setters
By JavaForums in forum NetBeansReplies: 0Last Post: 07-30-2007, 11:13 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks