1

I'm writing a code for an special case of hanoi towers , and now I have this problem that suddenly emerged out of nowhere !

Well , to have said it easily , the objects that I add to my arraylist do not match the objects that are in the arraylist !

I'm posting my code here , and I know it is long , but I have written a comment above the line where the problem is happening :

    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() ;
        }*/
    }
};

with the following input , I'm not getting what I expect !!! could someone please tell me what's wrong ?

edit : the problem is here :

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 *******************************************************************************

}

you see , I once print the objects I want to add to arraylist before adding them , and once after that , and the results are different , which shouldnt be !

sample input :

2

10

1

2 4 3

7 5 6

10 8 9

4

2

3 1 4

sample out put :

"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

// the output for the first printing , and the rest is the output for the second printing , and they are different as you see

2

3

1

4

2

3

1

4

p.s. : I have tested all the parsing units in my program and the problem happens in the start commented area !!! and by the way all the lines in input are in one column , I couldn’t fix it here !

4

1 回答 1

3

问题是您的所有Hanoi属性都是静态的。因此,它们在Hanoi. 也就是说,每次您执行此操作时,您new Hanoi(something, somethingElse)都会有效地销毁先前创建的Hanoi.

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

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

于 2013-10-18T16:11:26.153 回答