gpt4 book ai didi

java - 避免在java中嵌套foreach循环

转载 作者:行者123 更新时间:2023-12-02 03:25:04 24 4
gpt4 key购买 nike

自从我参加编码类(class)以来,已经有大约 20 年了。我有时会为了好玩而重新拿起它,但我的代码很笨重且效率低下。

我有一个只有 400 多个元素的数组。我目前已经在该数组上运行了嵌套的 foreach 循环。

import acm.program.ConsoleProgram;
import java.awt.Color;
import acm.io.IODialog;
import java.text.*;
import static java.lang.Math.*;
import java.util.*;

/** Tests to see if user color matches sample colors */

public class test extends ConsoleProgram

{

//defining sample colors

Color[] dmc =
{
new Color(255,255,255),
new Color(148,91,128),
new Color(206,148,186),
new Color(236,207,225),
new Color(243,218,228),

};

public void run()
{
average();

}

//averages three colors, then tests for match to given color

public void average()
{

//asks for user color
IODialog dialog = new IODialog();
int stitchRed= dialog.readInt("Enter red value: ");
int stitchGreen= dialog.readInt("Enter green value: ");
int stitchBlue= dialog.readInt("Enter blue value: ");
Color stitchColor= new Color(stitchRed,stitchGreen,stitchBlue);

//gets averages for dmc colors
for (Color i:dmc)
{
for (Color j:dmc)
{
for (Color k:dmc)
{
int indexI = Arrays.asList(dmc).indexOf(i);
int indexJ = Arrays.asList(dmc).indexOf(j);
int indexK = Arrays.asList(dmc).indexOf(k);
if(indexI <= indexJ && indexJ <= indexK)
{
int iRed = i.getRed();
int jRed = j.getRed();
int kRed = k.getRed();
int iGreen = i.getGreen();
int jGreen = j.getGreen();
int kGreen = k.getGreen();
int iBlue = i.getBlue();
int jBlue = j.getBlue();
int kBlue = k.getBlue();
int redAverage = (iRed+jRed+kRed)/3;
int greenAverage = (iGreen+jGreen+kGreen)/3;
int blueAverage = (iBlue+jBlue+kBlue)/3;
Color colorAverage = new Color(redAverage,greenAverage,blueAverage);

//tests to see if any thread average equals user color
if (colorAverage.equals(stitchColor))
{
println("The color match is: " + i + ", " + j + ", " + k);
}
}
}
}
}

println("no more matches");
}
}

编译得很好,但运行速度非常慢。

有没有更有效的方法来做到这一点?
可能是一种绕过嵌套的方法 - 其效果是:

for(颜色 i,j,k:dmc)

最佳答案

它等于@C0der的答案,只是对问题的变量使用相同的名称。

    for (int indexI = 0; indexI < dmc.length; indexI++) {
Color i = dmc[indexI];
for (int indexJ = indexI; indexJ< dmc.length; indexJ++) {
Color j = dmc[indexJ];
for (int indexK = indexJ; indexK< dmc.length; indexK++) {
Color k = dmc[indexK];
//....
}
}
}

编辑:

public void average() {
//asks for user color
IODialog dialog = new IODialog();
int stitchRed = dialog.readInt("Enter red value: ");
int stitchGreen = dialog.readInt("Enter green value: ");
int stitchBlue = dialog.readInt("Enter blue value: ");
Color stitchColor = new Color(stitchRed, stitchGreen, stitchBlue);
for (int indexI = 0; indexI < dmc.length; indexI++) {
Color i = dmc[indexI];
for (int indexJ = indexI; indexJ < dmc.length; indexJ++) {
Color j = dmc[indexJ];
for (int indexK = indexJ; indexK < dmc.length; indexK++) {
Color k = dmc[indexK];
int iRed = i.getRed();
int jRed = j.getRed();
int kRed = k.getRed();
int iGreen = i.getGreen();
int jGreen = j.getGreen();
int kGreen = k.getGreen();
int iBlue = i.getBlue();
int jBlue = j.getBlue();
int kBlue = k.getBlue();
int redAverage = (iRed + jRed + kRed) / 3;
int greenAverage = (iGreen + jGreen + kGreen) / 3;
int blueAverage = (iBlue + jBlue + kBlue) / 3;
Color colorAverage = new Color(redAverage, greenAverage, blueAverage);
//tests to see if any thread average equals user color
if (colorAverage.equals(stitchColor)) {
System.out.println("The color match is: " + i + ", " + j + ", " + k);
}
}
}
}

System.out.println("no more matches");
}

关于java - 避免在java中嵌套foreach循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39052193/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com