javaMap与arraylist怪异

knpiaxh1  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(325)

我需要另一双眼睛,因为我错过了一些东西。这是我的代码:。。。

import java.util.Scanner;
    import java.util.Map;
    import java.util.HashMap;
    import java.util.ArrayList;
    import java.io.*;
    import java.nio.file.*;

    public class TestMap {
        private Map<String, ArrayList<String>> validCommands = new HashMap<String, ArrayList<String>>();

        public TestMap( ) throws Exception {
            getCommands();
        }

        private void getCommands() throws Exception {
            Map<String,ArrayList<String>> dataMap = new HashMap<>();
            String nameFile = "." + File.separator + "TestMap.txt";
            FileInputStream fis = new FileInputStream( nameFile );
            Scanner input = new Scanner( fis );
            ArrayList<String> sub = new ArrayList<>();
            while(input.hasNext()) {
                String[] tt = {""};
                tt[0] = input.nextLine().replace( " ", "" ).toUpperCase();
                // Debug next 1 line
                System.out.println( "\nRead: " + tt[0] );
                CharSequence divider = ",";
                sub.clear();
                if( tt[0].contains( divider ) ) {
                    tt = tt[0].split( ",", 2 );
                    // Debug next 2 lines
                    System.out.print( "Split to " );
                    for( int i = 0; i<tt.length; i++ ) System.out.print( tt[i] + "/" );
                    if( tt.length > 1 ) {
                        for( int i = 1; i<tt.length; i++ ) {
                            sub.add( tt[i] );
                        }
                    }
                }
                // Debug next 2 lines
                System.out.println( "\nsub is now " + sub );
                System.out.println( "Now putting " + tt[0] + sub );
                dataMap.put( tt[0], sub );
            }
            input.close();
            // Debug next 3 lines
            System.out.println( "\nFinal result:" );
            for( String s : dataMap.keySet() ) System.out.println( s + "/" + dataMap.get( s ) );
            System.out.println();
        }

        public static void main( String[] args ) {
            try {
                TestMap testmap = new TestMap();
            }
            catch ( Exception e ) {
                System.exit( 1 );
            }
        }
    }

... 我使用的是windows,它编译起来没有问题c:\program files\java\jdk1.8.0\U 271\bin\javac'-xlint:unchecked .\testmap.java输入文件是:

Replace, Title, Description, Language
    Create
    Delete
    Change, Title, Description**

我期望的结果是按键的顺序排列,字符串数组如下:

DELETE/[]
    CREATE/[]
    CHANGE/[TITLE,DESCRIPTION]
    REPLACE/[TITLE,DESCRIPTION,LANGUAGE]

我得到的结果(使用debug print语句):

java TestMap

    Read: REPLACE,TITLE,DESCRIPTION,LANGUAGE
    Split to REPLACE/TITLE,DESCRIPTION,LANGUAGE/
    sub is now [TITLE,DESCRIPTION,LANGUAGE]
    Now putting REPLACE[TITLE,DESCRIPTION,LANGUAGE]

    Read: CREATE

    sub is now []
    Now putting CREATE[]

    Read: DELETE

    sub is now []
    Now putting DELETE[]

    Read: CHANGE,TITLE,DESCRIPTION
    Split to CHANGE/TITLE,DESCRIPTION/
    sub is now [TITLE,DESCRIPTION]
    Now putting CHANGE[TITLE,DESCRIPTION]

    Final result:
    DELETE/[TITLE,DESCRIPTION]
    CREATE/[TITLE,DESCRIPTION]
    CHANGE/[TITLE,DESCRIPTION]
    REPLACE/[TITLE,DESCRIPTION]

为什么所有键都有相同的数组列表?

liwlm1x9

liwlm1x91#

put arraylist sub=新建arraylist<>();在循环中,您正在更新相同的数组。这就是问题的根源。如果注意到使用的是同一个数组并清除该数组,则还会将同一个数组引用放入map值中。因此map值将包含所有键的最后一个array值。

import java.util.Scanner;
    import java.util.Map;
    import java.util.HashMap;
    import java.util.ArrayList;
    import java.io.*;
    import java.nio.file.*;

    public class TestMap {
        private Map<String, ArrayList<String>> validCommands = new HashMap<String, ArrayList<String>>();

        public TestMap( ) throws Exception {
            getCommands();
        }

        private void getCommands() throws Exception {
            Map<String,ArrayList<String>> dataMap = new HashMap<>();
            String nameFile = "." + File.separator + "TestMap.txt";
            FileInputStream fis = new FileInputStream( nameFile );
            Scanner input = new Scanner( fis );

            while(input.hasNext()) {
                 ArrayList<String> sub = new ArrayList<>();
                String[] tt = {""};
                tt[0] = input.nextLine().replace( " ", "" ).toUpperCase();
                // Debug next 1 line
                System.out.println( "\nRead: " + tt[0] );
                CharSequence divider = ",";

                if( tt[0].contains( divider ) ) {
                    tt = tt[0].split( ",", 2 );
                    // Debug next 2 lines
                    System.out.print( "Split to " );
                    for( int i = 0; i<tt.length; i++ ) System.out.print( tt[i] + "/" );
                    if( tt.length > 1 ) {
                        for( int i = 1; i<tt.length; i++ ) {
                            sub.add( tt[i] );
                        }
                    }
                }
                // Debug next 2 lines
                System.out.println( "\nsub is now " + sub );
                System.out.println( "Now putting " + tt[0] + sub );
                dataMap.put( tt[0], sub );
            }
            input.close();
            // Debug next 3 lines
            System.out.println( "\nFinal result:" );
            for( String s : dataMap.keySet() ) System.out.println( s + "/" + dataMap.get( s ) );
            System.out.println();
        }

        public static void main( String[] args ) {
            try {
                TestMap testmap = new TestMap();
            }
            catch ( Exception e ) {
                System.exit( 1 );
            }
        }
    }

相关问题