java - 从 Java ArrayList 中得到意想不到的结果

标签 java 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

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

最佳答案

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

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

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

关于java - 从 Java ArrayList 中得到意想不到的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19453963/

相关文章:

java - JList 复制选择

java - 如何将ArrayList打印成行和列?

java - Firebase 与 Room 持久性库

java - JAXB 自行重命名属性

java - 将扑克牌数组列表中的所有数值相加

java - 如何将字符串拆分为 ArrayList?

java - 使用 java 迭代 List<HashMap<String, String>> 值

java - Spring 启动 Web 应用程序 : Get Hibernate to use log4j

java - 在 Java 中访问 MidiDevice

java - 按字母顺序排序数组列表时遇到问题