2014-11-23 2 views
1

처음으로 Processing에서 ArrayList를 사용하기 때문에 몇 가지 문제가 발생합니다.ArrayList 처리 - 각 색인에 개별적으로 액세스하는 방법

저는 Ellipsee의 PVector (x, y 위치)를 저장하는 Arraylist를 만들었습니다.

내가 뭘 하려는지 매우 간단 생각하지만, ArrayLists에 대한 많은 정보를 찾을 수 없습니다.

코드 : ArrayList 위치;

void setup() 
{ 
    position= new ArrayList<Vectors>(); 
} 

void draw() 
{ 
    position.get(i).display(); //display ellipse 
} 

void mousePressed() 
{ 
    position.add(new Vectors(new PVector(mouseX, mouseY))); 
} 

그래서 때마다 마우스는 새로운 타원이 mouseX 쥐의 위치에 생성됩니다 눌려. 내가 뭘하고 싶은지, 나는 내가 타원형을 만들었을 때, 그것들을 클릭하거나 KeyPressed()를 사용하여 크기 나 색을 변경하기 위해 각각을 개별적으로 제어해야한다. 나는 당신의 PVector 개체가 이미 생성되고있어 xPosition과 yPosition의 두 공공 속성이 있으리라 믿고있어로

+0

코드를 추가하십시오. – jruizaranguren

답변

2

이 자동으로 컴파일되지 않습니다 : 기본적으로

// Initialise your arraylist 
ArrayList<PVector> listOfPVectors = new ArrayList<PVector>; 

// Objects can be added to your list as follows: 
PVector pvectorObject = new PVector(); 
listOfPVectors.add(pvectorObject); 

// The size of your ArrayList can be output as follows: 
println(listOfPVectors.size()); 

// You can iterate through every entry in the arraylist as follows: 
for(int index = 0; index < listOfPVectors.size(); index ++) { 
    println("X Position Value = " + listOfPVectors.get(index).xPosition); 
    println("Y Position Value = " + listOfPVectors.get(index).yPosition); 
} 

을 있습니다 (ArrayList.get를 사용 indexPosition) 메서드를 사용하여 ArrayList에서 원하는 모든 요소를 ​​검색합니다. 그런 다음 정상적으로 작업 할 수 있습니다.

희망이 도움이됩니다.

+0

안녕하세요 앤서니 당신의 대답에 감사드립니다. 처리에서 getXPosition()이 작동합니까? – Apollon1954

+0

안녕하세요, 처리를 위해 좀 더 명확하게 답변을 편집했습니다. 이제는 좀 더 명확 해지기를 바랍니다. getXPosition() 및 getYPosition()에 대한 getter 메소드를 제거했습니다. 이게 도움이되는지 알려주세요. – AJC24

+0

고맙습니다. Anthony 그것이 나를 도와줍니다. 그러나 나는 처리에서 값을 인쇄하는 것 : println ("Y Position Value ="+ listOfPVectors.get (index) .x); – Apollon1954

관련 문제