从Java ArrayList中获得意外结果

时间:2013-10-18 15:58:28

标签: java arraylist

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

好吧,为了轻松说出来,我添加到我的arraylist中的对象与arraylist中的对象不匹配!

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

    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

P.S。 :我已经测试了我程序中的所有解析单元,问题发生在开始注释区域!顺便说一句,输入中的所有行都在一列中,我无法解决这个问题!

1 个答案:

答案 0 :(得分:3)

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

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

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