3
public void setPunt(int index){
    if(index >= 0 && index < PuntenLijst.size()){
        x = 3;
        y = 5;
    }
}

I have got an array of object instances in an array list called PuntenLijst. With this method I want to use the index parameter to locate the object in the ArrayList and set the variables x and y that are defined in the object instance to 3 and 5.

This is how far I have gotten. Is there anyone who can help me out?

Thanks a lot!

1
  • Sorry but this is a little vague. Are you just saying you want, PutenLijst.get(index).x=3? Commented Oct 16, 2012 at 21:19

5 Answers 5

6

Create a setX and setY method in your object. Use them to change the value.

public void setPunt(int index){
if(index >= 0 && index < PuntenLijst.size()){
    PuntenLijst.get(index).setX(3);
    .... setY(5);
}

}

Sign up to request clarification or add additional context in comments.

Comments

1
public void setPunt(int index){
if(index >= 0 && index < PuntenLijst.size()){

  (ClassOfObjects)PuntenLijst.get(index).x=3;
  (ClassOfObjects)PuntenLijst.get(index).y=5;


  }
}

Comments

0

Ask the list for the Punt stored at the given index: See http://docs.oracle.com/javase/6/docs/api/java/util/List.html#get%28int%29.

Then use the methods provided by the Punt class to modify its internal state. My guess is that it has a setX() and a setY() methods (it should have those if you need to change its coordinates, anyway).

Comments

0

Did you mean something like this?

ArrayList<Object> PuntenLijst= new ArrayList<>(); // your arrayList

public void setPunt(int index){
    if(index >= 0 && index < PuntenLijst.size()){
        Object myObj = PuntenLijst.get(index);
        myObj.setX(3);
        myObj.setY(5);
    }
}

Comments

0

Check out the docs on ArrayList http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

In your case, you'd want to do the following:

   if(index >= 0 && index < puntenLijst.size()){
        puntenLijst.get(index).x = 3;
        puntenLijst.get(index).y = 5;
    }

Note the following changes: puntenLijst should not be capitalized. x and y must be public variables in order to assign values like above.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.