Web Hosting Talk







View Full Version : java~converting array to array list


gracie
04-28-2003, 09:35 AM
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();
}
}

rooshine
04-28-2003, 09:54 AM
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!

gracie
04-28-2003, 10:03 AM
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 .. ><

rooshine
04-28-2003, 10:42 AM
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.

gracie
04-28-2003, 10:58 AM
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...

rooshine
04-28-2003, 11:08 AM
If you can wait, I will type something up for you in an hour or so

gracie
04-28-2003, 11:15 AM
okay , yes i can wait ... i got 6 hrs, then i got to study for my two exams... thank you ...

ilyash
04-28-2003, 11:30 AM
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?

ilyash
04-28-2003, 11:30 AM
*messed up caps...
its ArrayList not arraylist

gracie
04-28-2003, 11:39 AM
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 ..

gracie
04-28-2003, 11:41 AM
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

rooshine
04-28-2003, 12:39 PM
here's a code fragment:

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:

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.

gracie
04-28-2003, 06:22 PM
roo i can't get it to subsit tutie ..
it own't work..
canu stick it in my code plz..

gracie
04-29-2003, 02:19 AM
roo dun worry about it i got it going now... ^^
thansk boy