Results 1 to 14 of 14
  1. #1
    Join Date
    Mar 2003
    Posts
    63

    java~converting array to array list

    I have a problem now , i need to incorporate an object of a type from java API,
    and i wanted to change the array to the array list thing... but it stuff up when i did it .. >< code below

    import java.util.*;

    public class SheepFinal {
    int ysheep;
    int xsheep;
    int dogxPos = 4; // x position of dog
    int dogyPos = 5; // y position of dog
    private int Energy = 10; //amount of energy in sheep
    double Length;
    int L;
    public int[] xsheepPos = new int [8]; // a list of x co-ordinates
    public int[] ysheepPos = new int [8]; // a list of y co-ordinates

    public void sheepWalk() {
    System.out.println("Sheep is walking");
    Energy = Energy - 2;
    }

    public void sheepEat() {
    Energy = Energy + 3;
    System.out.println("Sheep is eating grass");
    System.out.println("The sheep is at position" + "" + xsheepPos[xsheep] + "," + ysheepPos[ysheep]);
    }

    public void sheepDrink() {
    Energy = Energy + 2;
    System.out.println("Sheep is drinking water");
    System.out.println("The sheep is at position" + "" + xsheepPos[xsheep] + "," + ysheepPos[ysheep]);
    }

    // calculate the distance between the sheep and dog
    public void sheepDistance() {
    sheepCord();
    Length = Math.sqrt(Math.pow((xsheepPos[xsheep] - dogxPos),2) + Math.pow((ysheepPos[ysheep] - dogyPos),2));
    Length = Math.abs(Length); // make it a positive number
    L = (int)Math.round(Length); // rounds off to the nearest whole number
    }

    // stores some numbers into array
    public void sheepCord() {
    for (int xsheep= 0; xsheep < xsheepPos.length; xsheep++) {
    xsheepPos[xsheep] =(int)(Math.random()*8);
    }
    for (int ysheep = 0; ysheep < ysheepPos.length; ysheep++) {
    ysheepPos[ysheep] =(int)(Math.random()*8);
    }
    System.out.println("The sheep is at position" + "" + xsheepPos[xsheep] + "," + ysheepPos[ysheep]);
    }

    public void sheepRun() {
    System.out.println("Sheep is moving around the pasture to avoid dog");
    Energy = Energy - 5;
    sheepDistance();
    }

    // determine the energy level left in the sheep and action the sheep
    // takes if energy is high or low
    public void changeEnergy() {
    Energy = Energy - 1;
    if (Energy <= 5) {
    sheepEat();
    sheepDrink();
    } else {
    sheepWalk();
    }
    }

    // shows how close the dog is to the sheep and the action the sheep take when dog is too close
    public void sheepClose() {
    sheepDistance();
    if (L <= 4) {
    sheepRun();
    } else {
    sheepWalk();
    }
    }

    public static void main (String[] args) {
    SheepFinal s1 = new SheepFinal();
    s1.sheepWalk();
    s1.changeEnergy();
    s1.sheepClose();
    s1.changeEnergy();
    }
    }

  2. #2
    Join Date
    Apr 2003
    Location
    NYC
    Posts
    846
    I just looked over your code very quickly, and I don't see where you tried to implement an ArrayList... Maybe I missed it.

    I did notice in your public void sheepCord() method you access the xsheepPos[xsheep], ysheepPos[ysheep]) after the iteration. This will generate a runtime error as by this point xsheep and ysheep are == xsheepPos.length, rather than <. Also, you appear to to using these variables throughout your code, which will cause problems.

    But overall, you appear to be learning to use the API and docs quite well!

  3. #3
    Join Date
    Mar 2003
    Posts
    63
    okay that was my code, i still nee one check point, to finsih it off .. i was told to replace the arrayw ith an arraylist intead
    but i dunno how to do it without stuffing up the code again...
    can you help plz.....

    ps the APi was very hard to understand .. ><

  4. #4
    Join Date
    Apr 2003
    Location
    NYC
    Posts
    846
    ArrayList is part of the java.util package.

    http://java.sun.com/j2se/1.4.1/docs/api/

    It’s basic constructor is ArrayList list = new ArrayList();

    You add elements like this: list.add(Object o);

    You get elements like this: list.get(int index); // this returns the element at position index.

    The thing to remember about an ArrayList is that it holds objects. So if you want to add an int, for example, you would use the Integer wrapper:

    list.add(new Integer(14));

    When retreiving an element, it returns an object, so you need to cast it:

    Integer sheep=(Integer)list.get(1)

    Hope that helps.

    The API can be intimidating at first, but trust me, it will pay off. Once you get the foundations down and gain a little experience, you can learn quite a bit from just looking up the API.

  5. #5
    Join Date
    Mar 2003
    Posts
    63
    i read that api thing .. i dun get it roo............>< >< can u do it for me plz...>< i dunno hwo to convert my sheepxPOs and sheepy Pos array to that ... ><
    then send it back for other method to call.......

    i knoiw this sound really but can u type it up .... plz..roo pretty plz...

  6. #6
    Join Date
    Apr 2003
    Location
    NYC
    Posts
    846
    If you can wait, I will type something up for you in an hour or so

  7. #7
    Join Date
    Mar 2003
    Posts
    63
    okay , yes i can wait ... i got 6 hrs, then i got to study for my two exams... thank you ...

  8. #8
    Join Date
    Dec 2002
    Location
    NY, NY
    Posts
    3,974
    String[] array = {"hello","i","am","ilya"};
    arraylist al = new arraylist();
    for(int i=0; i<array.length; i++){
    al.add(array[i]);
    }

    is this what u wanted?

  9. #9
    Join Date
    Dec 2002
    Location
    NY, NY
    Posts
    3,974
    *messed up caps...
    its ArrayList not arraylist

  10. #10
    Join Date
    Mar 2003
    Posts
    63
    from above code, above . i want to change the array of sheepsPOs and sheepyPos to arraylist.. so i can have and object from the java api add to it ..

  11. #11
    Join Date
    Mar 2003
    Posts
    63
    from above code, above . i want to change the array of sheepsPOs and sheepyPos to arraylist.. so i can have and object from the java API add to it ..

    i missing thay point

  12. #12
    Join Date
    Apr 2003
    Location
    NYC
    Posts
    846
    here's a code fragment:
    Code:
    ArrayList list=new ArrayList(); //declare the ArrayList outside the loop.
    Random random=new Random();
    
    for(int i=0;i<length;i++) //here length is a variable you can change to alter the size of your list
    {
    	Point point = new Point(random.nextInt(9), random.nextInt(9)); //creates new point with 2 random ints 0-8.
    	list.add(point);
    }
    This will replace your two arrays sheepxPos and sheepyPos.

    I used the Point class because an ArrayList stores objects, not primitives, and using the Point class was easier than using two Integer wrappers for every point.

    Rather than have 2 arrays to hold your x and y coordinates, each point holds both:

    int x=point.getX();
    int y-point.getY();

    If you wanted to print out the position of your sheep using this ArrayList, it would look something like this:
    Code:
    for(int i=0;i<list.size();i++)
    {
    	Point point = (Point)list.get(i);
    	System.out.println("The sheep is at position " + point.getX() + ", " + point.getY());
    }
    Hope this is what you’re looking for.

  13. #13
    Join Date
    Mar 2003
    Posts
    63
    roo i can't get it to subsit tutie ..
    it own't work..
    canu stick it in my code plz..

  14. #14
    Join Date
    Mar 2003
    Posts
    63
    roo dun worry about it i got it going now... ^^
    thansk boy

Posting Permissions

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