正在跳过循环

时间:2013-12-30 13:54:50

标签: java for-loop

我这里的想法已经不多了。我想要的是非常简单,tempArray是由函数生成的五个字符串的arraylist。 for-each循环应该迭代通过twoArray,一个arrayList的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");
}

}

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

1 个答案:

答案 0 :(得分:1)

增强的for循环遍历给定iterator的所有元素,因为twoArray为空,没有任何内容可以迭代。