如何在java中更新arraylist中的数组中的元素

如何在java中更新arraylist中的数组中的元素,java,arrays,arraylist,Java,Arrays,Arraylist,我在Java中创建了一个ArrayList,并在其中放入了一些整数和一些double,然后添加了一个整数数组和一个double数组。现在我已经更改了数组中一个双精度的值,我想更新它。但是,我很难给出它的确切语法,因为它是ArrayList中数组中的一个位置 更具体地说,我有 ArrayList<Patch> patches; patches = new ArrayList<Patch>(); //the ArrayList of patches int[] VisP

我在Java中创建了一个ArrayList,并在其中放入了一些整数和一些double,然后添加了一个整数数组和一个double数组。现在我已经更改了数组中一个双精度的值,我想更新它。但是,我很难给出它的确切语法,因为它是ArrayList中数组中的一个位置

更具体地说,我有

ArrayList<Patch> patches; 
patches = new ArrayList<Patch>();  //the ArrayList of patches

int[] VisPatch;  //the array of integers that keeps track of the patchID of the visible patches
double[] formFactor;  //the array of doubles that keeps track of the formFactor for the visible patches

//these are brought in from a file using a scanner
int patchID = fin.nextInt(); //the patch number for each patch in the file
double emissionPower = fin.nextDouble();  //the emission power of the individual patch
double reflectance = fin.nextDouble();  //the reflectance of the individual patch
int numVisPatch = fin.nextInt();  //the number of patches that are 'visible' to this particular patch

//initialize the arrays that hold the visible patch parameters
VisPatch = new int[numVisPatch];
formFactor = new double[numVisPatch];

for(int i=0; i<numVisPatch; i++){
    VisPatch[i] = fin.nextInt(); //brought in from file
    formFactor[i] = fin.nextDouble();
}//end for loop

//create a new patch object from the numbers read in
patches.add(new Patch(emissionPower, reflectance, numVisPatch, VisPatch, formFactor));

//get the first visible patch in the VisPatch array
int adjacentPatchID = patches.get(maxKeyIndex).VisPatch[k]; //maxKeyIndex has been declared, and yes, we're in a for loop that uses k

//do some math on the emissionPower
double increment = 2;
double newEmissionPower = patches.get(adjacentPatchID).emissionPower + increment;

//now update the emission power of the patch
ummm...


但是我的IDE(我正在使用Eclipse)只是在所有东西下面放了一堆红色的曲线,并说我不知道我在说什么(主要是因为我不知道)。

您已经创建了一个
数组列表
;这是一个
列表
。意思事物列表(在您的例子中是,
Patch
objects)。您要么需要通过特定索引(ArrayList的一个功能)访问它,要么遍历它以找到您要查找的内容

JavaDocs是你的朋友:


与Oracle教程一样:

我假设您要更新的内容在
补丁
类中,而不是补丁列表中

你想通过ID获取补丁(假设你的ID)在这个例子中是它在列表中的位置;如果不是,你可以考虑使用<代码> map < /C> >,然后改变它:

Patch patchToUpdate = patches.get(adjacentPatchId);
patchToUpdate.emissionPower = newEmissionPower;

//or if it has a proper mutator method:
patchToUpdate.setEmissionPower(newEmissionPower);

红色消息是什么意思?如果您向我们提供Eclipse提供的错误,您会发现您会得到更快更好的帮助。错误消息说“类型ArrayList中的方法集(int,Patch)不适用于参数(int,double)是的,但不是访问ArrayList给我带来了问题,而是更新它给我带来了麻烦。我可以使用“get”访问任何我想要的索引。但是,如果我更改ArrayList中数组中的值,如何用新值替换旧值?
ArrayList.set()
获取索引和对象。它将对象存储在您提供的索引处。看来你想把它当作一个游戏来使用。除此之外。。。如果要修改的对象已在列表中,则无需再次调用
set()
;你只需从列表中获取并修改它-你正在处理对象引用。你是说我应该在我的补丁类中放置一个方法来找到正确的补丁并更新它的发射功率吗?@Tabitha:一点也不。这是一个两步操作:首先,从你的补丁列表中找到正确的补丁,然后更新该补丁。哦,哇,好吧,我明白你现在说的,我不敢相信我以前没有看到。我想很明显我是个编程新手,谢谢。
patches.set(get(adjacentPatchID).emissionPower, newEmissionPower);
patches.set(adjacentPatchID.emissionPower, newEmissionPower);
Patch patchToUpdate = patches.get(adjacentPatchId);
patchToUpdate.emissionPower = newEmissionPower;

//or if it has a proper mutator method:
patchToUpdate.setEmissionPower(newEmissionPower);