【问题标题】:ArrayList Processing - How to access each index separatelyArrayList 处理 - 如何分别访问每个索引
【发布时间】:2014-11-23 20:42:14
【问题描述】:

由于这是我第一次在处理中使用 ArrayList,我遇到了一些问题。

我创建了一个存储 Ellipsee 的 PVector(x,y 位置)的 Arraylist。

我想我想做的很简单,但我在 ArrayLists 上找不到太多信息。

代码: 数组列表位置;

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 mouseY 位置创建一个新椭圆。我想做的是,当我创建一个数量椭圆时,我需要通过单击它们或使用 KeyPressed() 来分别控制每个椭圆以更改其大小或颜色。

【问题讨论】:

  • 请添加您的代码。

标签: arraylist processing


【解决方案1】:

这不会自动编译,因为我假设您的 PVector 对象已经创建并且它具有 xPosition 和 yPosition 两个公共属性:

// 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 中检索您想要的任何元素。然后你就可以照常处理它了。

希望这会有所帮助!

【讨论】:

  • 您好安东尼,谢谢您的回答。 getXPosition() 是否在处理中工作?
  • 嗨,我编辑了我的答案以更清楚地适应处理。希望现在更清楚一点(我删除了 getXPosition() 和 getYPosition() 的 getter 方法)。让我知道这是否有帮助:)
  • 谢谢安东尼,它确实帮助了我。但是我认为打印处理中的值是: println("Y Position Value = " + listOfPVectors.get(index).x);
  • 这取决于您在 PVector 对象中对这些属性的称呼。在我上面的假设中,我称它们为 xPosition 和 yPosition,但如果你对它们使用了简单的 x 和 y 变量,那也没关系。改用这些。
  • 谢谢安东尼。我设法做到了。如果你能帮助我,只有一个问题。我使用 position.get(i).display(r*2); 管理在 mousePressed 上调整椭圆的大小。但是,当鼠标松开时,它会恢复正常。你知道如何解决这个问题吗?
猜你喜欢
  • 1970-01-01
  • 2015-04-16
  • 2017-09-19
  • 2013-01-29
  • 2019-04-28
  • 1970-01-01
  • 1970-01-01
  • 2016-06-06
  • 2021-10-28
相关资源
最近更新 更多