我正在为河内塔的特殊情况编写代码,现在我遇到了这个突然出现的问题!

好吧,说起来容易,我添加到阵列列表中的对象与阵列列表中的对象不匹配!

我在这里发布代码,我知道它很长,但是我在发生问题的行上方写了一条注释:

    import java.io.* ;
import java.util.* ;

class Tools
{
    public static ArrayList<Integer> refinedSplit(String line) // this function basically splits a String by " " and throws the garbage out
    {
        String[] args = line.split(" ") ;
        ArrayList<Integer> returnValue = new ArrayList<Integer>() ;
        for ( int i = 0 ; i < args.length ; i++ )
            if ( !args[i].equals(" ") && !args[i].equals("") )
                returnValue.add(Integer.parseInt(args[i])) ;
        return returnValue ;
    }
    public static final Integer numOfPegs = 3 ;
};

class Hanoi
{
    public static ArrayList<ArrayList<Integer>> pegs ;
    protected static Integer biggestTower ;
    protected static Integer minOfMoves ;
    protected static Integer d0 ;
    protected static Integer k0 ;
    protected static Integer[] pegIndex ;
    protected static Integer[] goalIndex ;
    protected static void pegIndexSetter ()
    {
        pegIndex = new Integer[biggestTower+1] ;
        for ( int i = 0 ; i < pegs.size() ; i++ )
            for ( int j = 0 ; j < (pegs.get(i)).size() ; j++ )
                pegIndex[(pegs.get(i)).get(j)] = i ;
    }
    public Integer getMinOfMoves () { return minOfMoves ; }
    protected static void goalIndexSetter( Integer n )
    {
        if ( n.equals(biggestTower) )
        {
            goalIndex[n] = pegIndex[n] ;
            return ;
        }
        goalIndexSetter( n + 1 ) ;
        if ( pegIndex[n+1] == goalIndex[n+1] )
            goalIndex[n] = goalIndex[n+1] ;
        else
            goalIndex[n] = 3 - pegIndex[n+1] - goalIndex[n+1] ;
    }
    protected static void determiner()
    {
        Integer k = pegIndex[biggestTower] ;
        double a = biggestTower ;
        minOfMoves = (int)Math.pow(2 , a) - 1 ;
        for ( int d = biggestTower ; d > 0 ; d-- )
        {
            if ( pegIndex[d].equals(k) )
            {
                a = d - 1 ;
                minOfMoves = minOfMoves - (int)Math.pow(2 , a ) ;
            }
            else
            {
                d0 = d ;
                k0 = k ;
                k = 3 - pegIndex[d] - k ;
            }
        }

    }
    public static ArrayList<Integer> unYekiPeg ( Integer a , Integer b )
    {
        for ( Integer i = 0 ; i < Tools.numOfPegs ; i++ )
            if ( !i.equals(a) && !i.equals(b) )
                return pegs.get(i) ;
        return new ArrayList<Integer>() ;
    }
    //protected static void save ( void ) ;
    protected static void move ( ArrayList<Integer> from , ArrayList<Integer> to )
    {
        if ( from.size() != 0 )
        {
            to.add(from.get(from.size()-1)) ;
            from.remove(from.size()-1) ;
        }
    }
    protected void sortMessedUpHanoi ( ArrayList<Integer> from , ArrayList<Integer> to , ArrayList<Integer> using , Integer n )
    {
        System.out.println("shitfacemotherfucker") ;
        if ( n.equals(d0) )
        {
            System.out.println("SHIIIIIIIT") ;
            move(from,to) ;
            return ;
        }
        if ( !pegIndex[n].equals(goalIndex[n]) )
        {
            System.out.println("SHIt") ;
            sortMessedUpHanoi(from,to,using,n-1) ;
            Hanoi.move(pegs.get(pegIndex[n]),pegs.get(goalIndex[n])) ;
            sortMessedUpHanoi(using,to,from,n-1) ;
        }
        else
            sortMessedUpHanoi(from,to,using,n-1) ;
    }
    public void sort()
    {
        sortMessedUpHanoi(pegs.get(pegIndex[biggestTower]),pegs.get(goalIndex[biggestTower]),unYekiPeg(pegIndex[biggestTower],goalIndex[biggestTower]),biggestTower) ;
    }
    public void print ()
    {
        for ( int i = 0 ; i < pegs.size() ; i++ )
            for ( int j = (pegs.get(i)).size()-1 ; j >= 0 ; j-- )
                System.out.println((pegs.get(i)).get(j)) ;
    }
    public Hanoi ( ArrayList<String> _pegs , Integer max ) // aman az input e sarekari !
    {
        pegs = new ArrayList<ArrayList<Integer>>() ;
        Integer[] firstIndex = new Integer[Tools.numOfPegs] ;
        Integer[] lastIndex = new Integer[Tools.numOfPegs] ;
        Integer counter = 0 ;
        for ( int i = 0 ; i < (_pegs.get(_pegs.size()-1)).length() ; i++ )
        {
            if ( counter == Tools.numOfPegs )
                break ;
            if ( (_pegs.get(_pegs.size()-1)).charAt(i) != ' ' )
            {
                firstIndex[counter] = i ;
                while ( i < (_pegs.get(_pegs.size()-1)).length() && (_pegs.get(_pegs.size()-1)).charAt(i) != ' ' )
                    i++ ;
                lastIndex[counter] = i ;
                counter++ ;
            }
        }
        for ( int i = 0 ; i < Tools.numOfPegs ; i++ )
        {
            ArrayList<Integer> tempArray = new ArrayList<Integer>() ;
            for ( int j = _pegs.size() - 1 ; j >= 0 ; j-- )
            {
                ArrayList<Integer> temp = new ArrayList<Integer>() ;
                if ( lastIndex[i] < (_pegs.get(j)).length() )
                    temp = Tools.refinedSplit((_pegs.get(j)).substring(firstIndex[i],lastIndex[i])) ;
                else if ( firstIndex[i] < (_pegs.get(j)).length() )
                    temp = Tools.refinedSplit((_pegs.get(j)).substring(firstIndex[i])) ;
                if ( temp.size() == 0 )
                    break ;
                else if ( (temp.get(0)).equals(0) )
                {
                    pegs.add(new ArrayList<Integer>()) ;
                    break ;
                }
                else if ( temp.get(0) <= max )
                    tempArray.add(temp.get(0)) ;
            }
            pegs.add(tempArray) ;
        }
        biggestTower = max ;
        pegIndexSetter() ;
        goalIndex = new Integer[biggestTower+1] ;
        goalIndexSetter(1) ;
        determiner() ;
        /*//testing bitch
        System.out.println("-----------------------------------------------------") ;
        for ( int i = 0 ; i < goalIndex.length ; i++ )
            System.out.println("disk no : "+i+" goalIndex is : "+goalIndex[i]) ;
        System.out.println("d0 is : " + d0 + " and k0 is : " + k0+ " and min of moves is : " + minOfMoves ) ;
        System.out.println("-----------------------------------------------------") ;
        //end of testing bitch*/
    }
    public Hanoi ( ArrayList<ArrayList<Integer>> _pegs )
    {
        pegs = _pegs ;
    }
    public Hanoi () {}
};

public class MessedUpHanoi
{
    public static ArrayList<Hanoi> readAndParseInput ()
    {
        //reading raw input
        ArrayList<String> tempDecks = new ArrayList<String>() ;
        Scanner reader = new Scanner(System.in) ;
        while ( reader.hasNextLine() )
        {
            tempDecks.add(reader.nextLine()) ;
        }
        Integer numOfDecks = Integer.parseInt(tempDecks.get(0)) ;
        // from this line , I'm trying to separate my Hanois !!
        ArrayList<ArrayList<String>> decks = new ArrayList<ArrayList<String>>() ;
        Integer bookmark[] = new Integer[numOfDecks] ;
        Integer counter = 0 ;
        for ( int i = 1 ; i < tempDecks.size()-1 ; i++ )
        {
            if ( counter == numOfDecks )
                break ;
            if ( (Tools.refinedSplit(tempDecks.get(i))).get(0) >= (Tools.refinedSplit(tempDecks.get(i+1))).get(0) && (Tools.refinedSplit(tempDecks.get(i))).size() == 1  )
            {
                bookmark[counter] = i ;
                counter++ ;
            }
        }

        for ( int i = 0 ; i < bookmark.length ; i++ )
        {
            ArrayList<String> tempArrayList = new ArrayList<String>() ;
            if ( i == bookmark.length - 1 )
            {
                for ( int j = bookmark[i]+1 ; j < tempDecks.size() ; j++ )
                    tempArrayList.add(tempDecks.get(j)) ;
            }
            else
            {
                for ( int j = bookmark[i]+1 ; j < bookmark[i+1] ; j++ )
                    tempArrayList.add(tempDecks.get(j)) ;
            }
            decks.add(tempArrayList) ;
        }
        //end of separation of Hanois
        for ( int i = 0 ; i < decks.size() ; i++ )
        {
            for ( int j = 0 ; j < (decks.get(i)).size() ; j++ )
                System.out.println("\"" + (decks.get(i)).get(j) + "\"") ;
            System.out.println("___________________") ;
        }
        //now converting every deck to a Hanoi instance
        ArrayList<Hanoi> returnValue = new ArrayList<Hanoi>() ;
        // the problem is here **************************************************************************
        for ( int i = 0 ; i < decks.size() ; i++ )
        {
            Hanoi h = new Hanoi(decks.get(i),(Tools.refinedSplit(tempDecks.get(bookmark[i]))).get(0)) ;
            h.print() ;
            returnValue.add(h) ;
        }
        for ( int i = 0 ; i < returnValue.size() ; i++ )
            (returnValue.get(i)).print() ;
        return returnValue ;
        // till here *******************************************************************************
    }

    public static void main ( String [] args )
    {
        System.out.println("This program is designed to sort messed up Hanois") ;
        ArrayList<Hanoi> badHanoi = readAndParseInput() ;
        if ( args.length != 0 )
        {
            if ( args[0].equals("-n") )
            {
                for ( int i = 0 ; i < badHanoi.size() ; i++ )
                    System.out.println((badHanoi.get(i)).getMinOfMoves()) ;
            }
        }
        /*for ( int i = 0 ; i < badHanoi.size() ; i++ )
        {
            (badHanoi.get(i)).sort() ;
            (badHanoi.get(i)).print() ;
        }*/
    }
};


与以下输入,我没有得到我所期望的!
有人可以告诉我怎么了吗?

编辑:问题在这里:

ArrayList<Hanoi> returnValue = new ArrayList<Hanoi>() ;
// the problem is here **************************************************************************
for ( int i = 0 ; i < decks.size() ; i++ )
{
    Hanoi h = new Hanoi(decks.get(i),(Tools.refinedSplit(tempDecks.get(bookmark[i]))).get(0)) ;
    h.print() ;
    returnValue.add(h) ;
}
for ( int i = 0 ; i < returnValue.size() ; i++ )
    (returnValue.get(i)).print() ;
return returnValue ;
// till here *******************************************************************************


}

您会看到,我曾经在添加对象之前先打印要添加到arraylist的对象,然后再打印一次,结果是不同的,这不应该!

样本输入:

2

10

1个

2 4 3

7 5 6

10 8 9

4

2

3 1 4

放出样品:

“ 1”

“ 2 4 3”

“ 7 5 6”

“ 10 8 9”



“ 2”

“ 3 1 4”



1个

2

7

10

4

5

8

3

6

9

2

3

1个

4

//第一次打印的输出,其余的是第二次打印的输出,如您所见,它们是不同的

2

3

1个

4

2

3

1个

4

ps。 :我已经测试了程序中的所有解析单元,并且问题出现在开始注释区域!顺便说一下,输入中的所有行都在一列中,我无法在这里修复它!

最佳答案

问题是您的所有Hanoi属性都是静态的。因此,它们在所有Hanoi实例之间共享。也就是说,每次执行new Hanoi(something, somethingElse)时,都会有效地破坏先前创建的Hanoi

您需要从某些static属性(如果不是全部)中删除Hanoi关键字。

您可以在Sun / Oracle的Java tutorial中获得有关static关键字的更多信息。

08-15 19:30