gpt4 book ai didi

java - For 循环被跳过

转载 作者:行者123 更新时间:2023-12-01 13:40:30 25 4
gpt4 key购买 nike

我的想法已经用完了。我想要的很简单, tempArray 是由函数生成的五个字符串的数组列表。 for-each 循环应该迭代twoArray(arrayList 的数组列表),检查当前tempArray 的值是否存在,如果不存在,则添加当前tempArray,但由于某种原因它被跳过。

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package pal;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;

/**
*
* @author Weatherman
*/
public class Main {
public static int graphCount = 0;
public static ArrayList<String> testArray = new ArrayList<String>();
//public static
public static void generateCombinations(ArrayList<String> listEdges,ArrayList<String> tempArray, int start, int end,int index,int r,int numberOfNodes, int numberOfC, int numberOfN, int numberOfO)
{

int[] nodeCounter = new int[numberOfNodes+1];
int[] nodeCounter2 = new int[numberOfNodes+1];
int[] nodeCounter3 = new int[numberOfNodes+1];
ArrayList<String> iteratorArray = new ArrayList<String>(r);
ArrayList<String> finalArray = new ArrayList<String>(r);
int notAllNodesBreaker = 0;
int tooManyConnectionsBreaker = 0;
int notEnoughCycleEdgesBreaker = 0;
int notACycleBreaker = 0;
//ArrayList<ArrayList<String>> oneArray = new ArrayList<ArrayList<String>>();
ArrayList<ArrayList<String>> twoArray = new ArrayList<ArrayList<String>>();

if(index==r)
{
for(int k=0;k<r;k++)
{
String[] stringArray = tempArray.get(k).split(" ");
int temp1 = Integer.parseInt(stringArray[0]);
int temp2 = Integer.parseInt(stringArray[1]);
nodeCounter[temp1]++;
nodeCounter[temp2]++;
}

for(int l=1;l<=numberOfNodes;l++)
{
if(nodeCounter[l]==0)
{
notAllNodesBreaker = 1;
}
}
for(int l=1;l<=numberOfC;l++)
{
if(nodeCounter[l]>4)
{
tooManyConnectionsBreaker = 1;
}
}
for(int l=numberOfC+1;l<=numberOfC+numberOfN;l++)
{
if(nodeCounter[l]>3)
{
tooManyConnectionsBreaker = 1;
}
}
for(int l=numberOfC+numberOfN+1;l<=numberOfNodes;l++)
{
if(nodeCounter[l]>2)
{
tooManyConnectionsBreaker = 1;
}
}

//eliminace duplicitnich hran
outer: for(String element : tempArray)
{
for(String inElement : iteratorArray)
{
if(element.equals(inElement))
{
continue outer;
}
}
iteratorArray.add(element);
}
if(iteratorArray.size()<3)
{
notEnoughCycleEdgesBreaker = 1;
}

//hledani single elementu
for(String inElement : iteratorArray)
{
String[] stringArray = inElement.split(" ");
int temp1 = Integer.parseInt(stringArray[0]);
int temp2 = Integer.parseInt(stringArray[1]);
nodeCounter2[temp1]++;
nodeCounter2[temp2]++;
}


//vyrazeni single elementu

for(String inElement : iteratorArray)
{
String[] stringArray = inElement.split(" ");
int temp1 = Integer.parseInt(stringArray[0]);
int temp2 = Integer.parseInt(stringArray[1]);

if(nodeCounter2[temp1] == 1 || nodeCounter2[temp2] == 1)
{

}
else
{
finalArray.add(inElement);
}
}



if(finalArray.size()<3)
{
notEnoughCycleEdgesBreaker = 1;
}

//hledani cyklu
for(String inElement : finalArray)
{
String[] stringArray = inElement.split(" ");
int temp1 = Integer.parseInt(stringArray[0]);
int temp2 = Integer.parseInt(stringArray[1]);
nodeCounter3[temp1]++;
nodeCounter3[temp2]++;
}

for(int l=1;l<=numberOfNodes;l++)
{
if(nodeCounter3[l]==2||nodeCounter3[l]==0)
{

}
else
{
notACycleBreaker = 1;
}
}

for(int j=0;j<r;j++)
{

if(tempArray.equals(testArray))
{
return;
}
else if(notAllNodesBreaker == 1 || tooManyConnectionsBreaker == 1 || notEnoughCycleEdgesBreaker == 1 || notACycleBreaker == 1)
{
return;
}
else
{
//System.out.print(tempArray.get(j));
//System.out.print(";");
}
}
//oneArray.add(tempArray);
//outer: for(ArrayList<String> element : oneArray)
// {

这是给我带来麻烦的 for 循环:

           for(ArrayList<String> element : twoArray)
{
if(tempArray.equals(element))
{
//continue outer;

}
else
{
twoArray.add(tempArray);
}
}

这是其余的代码:

          for(int x=0;x<twoArray.size();x++)
{
for(int y=0;y<tempArray.size();y++)
{
System.out.print(twoArray.get(x).get(y));
System.out.print(";");
}
}
System.out.print("\n");
testArray = new ArrayList<String>(tempArray);
return;

}

for(int i=start; i<=end && end-i+1 >=r-index; i++)
{
tempArray.set(index, listEdges.get(i));
generateCombinations(listEdges, tempArray, i+1, end, index+1, r, numberOfNodes,numberOfC,numberOfN,numberOfO);
}


}

/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br = new BufferedReader(new FileReader(new File("././example.in")));

String[] parts = br.readLine().split("(?=[A-Z])|(?<=[A-Z])");
/*for(int i=0; i<parts.length;i++)
{
System.out.print(parts[i]);
System.out.print("\n");

}*/

int cCount = 0;
int hCount = 0;
int nCount = 0;
int oCount = 0;

ArrayList<String> listOfEdges = new ArrayList<String>();


for(int i=0; i<parts.length;i++)
{
if(parts[i].equals("C"))
{
cCount = Integer.parseInt(parts[i+1]);
}
if(parts[i].equals("H"))
{
hCount = Integer.parseInt(parts[i+1]);
}
if(parts[i].equals("N"))
{
nCount = Integer.parseInt(parts[i+1]);
}
if(parts[i].equals("O"))
{
oCount = Integer.parseInt(parts[i+1]);
}
//System.out.print(parts[i]);

}
int numberOfEdges = (4*cCount + hCount + 3*nCount + 2*oCount)/2-hCount;
int r = numberOfEdges;
int nodeNum = cCount + nCount + oCount;


for(int i=1;i<=(cCount+nCount+oCount-1);i++)
{
for(int j=i+1;j<=cCount+nCount+oCount;j++)
{
listOfEdges.add(Integer.toString(i) + " " + Integer.toString(j));
listOfEdges.add(Integer.toString(i) + " " + Integer.toString(j));
if(j<=cCount+nCount)
{
listOfEdges.add(Integer.toString(i) + " " + Integer.toString(j));
}
}

}
/*listOfEdges.add("1 2");
listOfEdges.add("1 2");
listOfEdges.add("1 3");
listOfEdges.add("2 3");
listOfEdges.add("2 4");*/

listOfEdges.trimToSize();
ArrayList<String> returnArray = new ArrayList<String>(r);
for(int i=0; i<r ;i++)
{
returnArray.add(null);
}
generateCombinations(listOfEdges, returnArray, 0, listOfEdges.size()-1, 0, r, nodeNum,cCount,nCount,oCount);




















System.out.print("\n");
System.out.print("cCount: ");
System.out.print(cCount);
System.out.print("\n");
System.out.print("hCount: ");
System.out.print(hCount);
System.out.print("\n");
System.out.print("nCount: ");
System.out.print(nCount);
System.out.print("\n");
System.out.print("oCount: ");
System.out.print(oCount);
System.out.print("\n");
System.out.print("\n");
System.out.print(numberOfEdges);
System.out.print("\n");
}

}

它只是在 Debug模式下跳过它,没有错误或任何东西。非常感谢任何帮助或提示。

最佳答案

增强的 for 循环迭代给定 iterator 的所有元素,由于您的 twoArray 为空,因此没有任何可迭代的内容。

关于java - For 循环被跳过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20840967/

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