Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 一种算法的优化_Java_Algorithm_Data Structures - Fatal编程技术网

Java 一种算法的优化

Java 一种算法的优化,java,algorithm,data-structures,Java,Algorithm,Data Structures,我的代码读取文本文件并创建boolean类型的输入数组[]。其大小约为100000-300000件。现在我面临的问题是创建所有大小为N,3>=N>=9的子集,这些子集具有连续的真值 例如,对于N=3,如果所有3个true都在连续索引中,则[true][true][true]是必需的子集 虽然我创建了一个算法,但它非常慢。我需要一个更快更有效的解决方案 请提出一些想法 public static void createConsecutivePassingDays() {

我的代码读取文本文件并创建
boolean
类型的输入数组[]。其大小约为100000-300000件。现在我面临的问题是创建所有大小为N,3>=N>=9的子集,这些子集具有连续的真值

例如,对于N=3,如果所有3个true都在连续索引中,则[true][true][true]是必需的子集

虽然我创建了一个算法,但它非常慢。我需要一个更快更有效的解决方案

请提出一些想法

 public static void createConsecutivePassingDays()
    {       
        for (String siteName  : sitesToBeTestedList.keySet())
        {
            System.out.println("\n*****************Processing for Site--->"+siteName+" ***********************");

            LinkedHashMap<String,ArrayList<String>> cellsWithPassedTripletsDates=new LinkedHashMap<String, ArrayList<String>>();

            for (String cellName : sitesToBeTestedList.get(siteName))
            {

                System.out.println("\n*****************Processing for Cell--->"+cellName+" ***********************");

                boolean failed=false;

                ArrayList<String> passedDatesTriplets=new ArrayList<String>();
                int consecutiveDays=0;
                String tripletDate="";
                String prevDate_day="";
                String today_Date="";

                for (String date : cellDateKpiMetOrNotMap.get(cellName).keySet())
                {
                    System.out.println("\nprocessing for Date-->"+date);
                    if(!(prevDate_day.trim().equals("")))
                        today_Date=getNextDay(prevDate_day.substring(0, prevDate_day.lastIndexOf('_')));

                    if(Connection.props.getProperty("INCLUDE_WEEKENDS").equalsIgnoreCase("FALSE"))
                    {
                        if(date.endsWith("SAT") || date.endsWith("SUN") || (!(date.substring(0, date.lastIndexOf('_')).equalsIgnoreCase(today_Date))))
                        {
                            if(consecutiveDays >= Reader.days)
                            {
                                passedDatesTriplets.add(tripletDate);
                            }

                            tripletDate="";
                            consecutiveDays=0;
                            prevDate_day=date;
                            continue;
                        }
                    }


                    if(cellDateKpiMetOrNotMap.get(cellName).get(date).equalsIgnoreCase("TRUE"))
                    {

                        if(tripletDate.equals(""))
                            tripletDate=date;
                        else
                            tripletDate+="#"+date;

                        consecutiveDays++;

                    }
                    else
                    {
                        failed=true;
                        if(consecutiveDays >= Reader.days)//kd
                        {
                            System.out.println("Triplet to be added-->"+tripletDate);
                            passedDatesTriplets.add(tripletDate);
                        }
                        tripletDate="";
                        consecutiveDays=0;
                    }

                    prevDate_day=date;
                }

                if(!failed)
                    passedDatesTriplets.add(tripletDate);
                else
                {
                    if(tripletDate.trim().split("#").length >= Reader.days)
                    {
                        passedDatesTriplets.add(tripletDate);
                    }
                }

                cellsWithPassedTripletsDates.put(cellName, passedDatesTriplets);

            }

            siteItsCellsWithPassedDates.put(siteName, cellsWithPassedTripletsDates);

        }

        System.out.println("\n************************************************SITES***************************************");
        for (String site : siteItsCellsWithPassedDates.keySet())
        {
            System.out.println("\n********************Site="+site+" ***********************");
            for (String cellName : siteItsCellsWithPassedDates.get(site).keySet())
            {
                System.out.println("\nCellName="+cellName);
                System.out.println(siteItsCellsWithPassedDates.get(site).get(cellName));
            }
            System.out.println("***********************************************************");
        }
        System.out.println("********************************************************************************************");
    }
public static void createConcertivePassingDays()
{       
对于(字符串siteName:sitesToBeTestedList.keySet())
{
System.out.println(“\n*******************处理站点-->”+siteName+“*************************”;
LinkedHashMap CellswithPassedTriplesDates=新LinkedHashMap();
for(字符串cellName:sitesToBeTestedList.get(siteName))
{
System.out.println(“\n****************处理单元-->”+cellName+“**********************”;
布尔失败=错误;
ArrayList passedDatesTriplets=新ArrayList();
int连续天数=0;
字符串tripletDate=“”;
字符串prevDate_day=“”;
字符串today_Date=“”;
for(字符串日期:cellDateKpiMetOrNotMap.get(cellName.keySet())
{
System.out.println(“\n处理日期-->”+日期);
如果(!(prevDate_day.trim()等于(“”))
today\u Date=getNextDay(prevDate\u day.substring(0,prevDate\u day.lastIndexOf)(“u”);
if(Connection.props.getProperty(“INCLUDE_WEEKENDS”).equalsIgnoreCase(“FALSE”))
{
if(date.endsWith(“SAT”)| date.endsWith(“SUN”)| |(!(date.substring(0,date.lastIndexOf(“u”)).equalsIgnoreCase(今日日)))
{
如果(连续天数>=读卡器天数)
{
passedDatesTriplets.add(tripletDate);
}
tripletDate=“”;
连续天数=0;
prevDate_day=日期;
继续;
}
}
if(cellDateKpiMetOrNotMap.get(cellName.get(date.equalsIgnoreCase)(“TRUE”))
{
if(三重日期等于(“”)
tripletDate=日期;
其他的
tripletDate+=“#”+日期;
连续天数++;
}
其他的
{
失败=真;
如果(连续天数>=Reader.days)//kd
{
System.out.println(“要添加的三元组-->”+三元组日期);
passedDatesTriplets.add(tripletDate);
}
tripletDate=“”;
连续天数=0;
}
prevDate_day=日期;
}
如果(!失败)
passedDatesTriplets.add(tripletDate);
其他的
{
if(tripletDate.trim().split(“#”).length>=Reader.days)
{
passedDatesTriplets.add(tripletDate);
}
}
CellswithPassedTriplesDates.put(cellName,passedDatesTriplets);
}
siteItsCellsWithPassedDates.put(siteName,CellswithPassedTriplesDates);
}
System.out.println(“\n*********************************************************************************************************************************************************************************************************************************************************站点”);
对于(字符串站点:siteItsCellsWithPassedDates.keySet())
{
System.out.println(“\n**********************站点=“+Site+”****************************”);
for(字符串cellName:siteItsCellsWithPassedDates.get(site.keySet())
{
System.out.println(“\nCellName=“+cellName”);
System.out.println(siteItsCellsWithPassedDates.get(site.get(cellName));
}
System.out.println(“****************************************************************************************”);
}
System.out.println(“*******************************************************************************************************************************************************************************************************************************”);
}

我将建议您创建一个stringbuilder,并为添加到布尔数组中的每个“true”值添加1,为添加的每个“false”值添加0。因此,stringbuilder将具有1和0的序列。然后使用indexOf(“111”)获取三个连续“true”值的起始索引,它将是stringbuilder和布尔数组中的起始索引。

我建议您创建一个stringbuilder,并为添加到布尔数组中的每个“true”值添加1,为添加的每个“false”值添加0。因此,stringbuilder将具有1和0的序列。然后使用indexOf(“111”)获取三个连续的“true”值的起始索引,它将是stringbuilder和布尔数组中的起始索引。

首先,我将远离
数组[boolean]
a
位集的内存效率更高,我希望它在您的情况下也更快。因为它将更好地利用缓存。看

对于算法:

遍历数据结构。 当遇到第一个
true
时,记住它的位置(
start
),直到到达
false
。这是位置
end
在这一点上,您就有了
true
值的连续间隔的开始和结束,这基本上就是您的结果。您可以从
start
end-n
获取子集int max = array.length - N; int index = 0; boolean valid = true; while (index < max) { valid = true; for (check = index; check<index+N; check++){ valid = valid && array[check]; } if (valid) { // you got a continous sequence of true of size N ; index++; } else { index = index + N; } }