使用if else语句对数组进行排序

时间:2017-10-23 19:03:01

标签: java arrays sorting ascii

所以,我正在研究java中的一个项目,我必须在其中输入一个字符串并计算字符串中字符的频率。然后,我必须将字符分为四类:文本,空格,数字和符号。我正在寻找一种方法来做到这一点。为了计算字符串中的频率,这是我到目前为止所做的:

    char[] charArray = userSort.toCharArray();
        char tempChar;
        for (int i = 0; i < charArray.length; i++) {
            for (int j = 0; j < charArray.length; j++) {
                if (charArray[i] < charArray[j]) {
                    tempChar = charArray[i];
                    charArray[i] = charArray[j];
                    charArray[j] = tempChar;]

我也有关于如何使用if-else语句对这些语句进行排序和打印的概念。

 int whiteSpace = 0;
            int textual = 0;
            int numerical = 0;
            int symbols = 0;

        if() {
            whiteSpace++;
        }

        else if (){
            textual++;
            }

        else if (){
            numerical++;
        }
        else {
            symbols++;
        }
            System.out.println("Textual Character count: " + textual);
            System.out.println("Numerical Character count: " + numerical);
            System.out.println("Whitespace Character count: " + whiteSpace);
            System.out.println("Symbol character count: " + symbols);

我只是不确定我使用if else语句将其保留在特定类别中。我也不确定如何将两个代码组合在一起。感谢任何帮助,谢谢。

另外,我无法使用内置 java排序功能或类似的东西。

3 个答案:

答案 0 :(得分:0)

它应该像迭代charArray一样简单,然后使用if / else。 为了知道元素是什么,你应该使用ascii赋值表https://www.dotnetperls.com/ascii-java

   for (int i = 0; i < charArray.length; i++) {
       int asciiValue = int(charValue);

       if(asciiValue == 11 || (asciiValue >= 28 && asciiValue <= 31) {
           whiteSpace++;
       }
   }

将相同的内容应用于文本(65 - 90和97 - 122),数字(48 - 57)和符号

答案 1 :(得分:0)

以下是您的起点,使用地图存储计数:

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;

public class TestCount {

    public static void main( String[] args ) {

        String myString = "abc abc abc abc aaabbbccc 0000 *&% a";
        int whiteSpace = 0;
        int textual = 0;
        int numerical = 0;
        int symbols = 0;

        char[] data = myString.toCharArray();
        Arrays.sort( data );

        Map<Character, Integer> countMap = new LinkedHashMap<>();
        for ( char c : data ) {
            if ( countMap.containsKey( c ) ) {
                countMap.put( c, countMap.get( c ) + 1 );
            } else {
                countMap.put( c, 1 );
            }
        }

        for ( Map.Entry<Character, Integer> e : countMap.entrySet() ) {

            char key = e.getKey();
            System.out.printf( "%c -> %d occurences\n", e.getKey(), e.getValue() );

            if ( ( key >= 'a' && key <= 'z' ) || ( key >= 'A' && key <= 'Z' ) ) {
                textual += e.getValue();
            } else if ( key >= '0' && key <= '9' ) {
                numerical += e.getValue();
            } else if ( key == ' ' ) {
                whiteSpace += e.getValue();
            } else {
                symbols += e.getValue();
            }

        }

        System.out.printf( "%d are textual characters\n", textual );
        System.out.printf( "%d are numerical characters\n", numerical );
        System.out.printf( "%d are whitespace characters\n", whiteSpace );
        System.out.printf( "%d are symbol characters\n", symbols );

    }

}

修改

这是一个简单而肮脏的解决方案,不使用内置功能,如排序和映射。它将char代码(表示char的整数)映射到数组位置。由于字符是有序的,因此数据数组的最后一个字符是具有较大代码的字符,并且将是最后一个有效的数组位置。

public class TestCount {

    public static void main( String[] args ) {

        String myString = "abc abc abc abc aaabbbccc 0000 *&% a";
        int whiteSpace = 0;
        int textual = 0;
        int numerical = 0;
        int symbols = 0;

        char[] data = myString.toCharArray();
        mySort( data );

        // a counting array. it will waste a lot of space, but it will work
        int[] countArray = new int[ (int) data[data.length-1] + 1 ];

        for ( char c : data ) {
            countArray[ (int) c ]++;
        }

        char lastChar = '\0';
        for ( char c : data ) {

            if ( c != lastChar ) {
                System.out.printf( "%c -> %d occurences\n", c, countArray[ (int) c ] );
            }

            if ( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) ) {
                textual++;
            } else if ( c >= '0' && c <= '9' ) {
                numerical++;
            } else if ( c == ' ' ) {
                whiteSpace++;
            } else {
                symbols++;
            }

            lastChar = c;

        }

        System.out.printf( "%d are textual characters\n", textual );
        System.out.printf( "%d are numerical characters\n", numerical );
        System.out.printf( "%d are whitespace characters\n", whiteSpace );
        System.out.printf( "%d are symbol characters\n", symbols );

    }

    public static void mySort( char[] array ) {
        for ( int i = 0; i < array.length; i++ ) {
            for ( int j = 0; j < array.length-1; j++ ) {
                if ( array[j] > array[j+1] ) {
                    char t = array[j];
                    array[j] = array[j+1];
                    array[j+1] = t;
                }
            }
        }
    }

}

编辑2:稍微节省空间的方法:

public class TestCount {

    public static void main( String[] args ) {

        String myString = "abc abc abc abc aaabbbccc 0000 *&% a";
        int whiteSpace = 0;
        int textual = 0;
        int numerical = 0;
        int symbols = 0;

        char[] data = myString.toCharArray();
        mySort( data );

        // a counting array. it will waste a lot of space, but it will work
        int[] countArray = new int[ (int) data[data.length-1] + 1 - (int) data[0]  ];

        for ( char c : data ) {
            countArray[ map(data[0], c) ]++;
        }

        char lastChar = '\0';
        for ( char c : data ) {

            if ( c != lastChar ) {
                System.out.printf( "%c -> %d occurence(s)\n", c, countArray[ map(data[0], c)  ] );
            }

            if ( c >= 'A' && c <= 'z' ) {
                textual++;
            } else if ( c >= '0' && c <= '9' ) {
                numerical++;
            } else if ( c == ' ' ) {
                whiteSpace++;
            } else {
                symbols++;
            }

            lastChar = c;

        }

        System.out.printf( "%d are textual characters\n", textual );
        System.out.printf( "%d are numerical characters\n", numerical );
        System.out.printf( "%d are whitespace characters\n", whiteSpace );
        System.out.printf( "%d are symbol characters\n", symbols );

    }

    public static int map( char leftBoundary, char charToMap ) {
        return (int) charToMap - (int) leftBoundary;
    }

    public static void mySort( char[] array ) {
        for ( int i = 0; i < array.length; i++ ) {
            for ( int j = 0; j < array.length-1; j++ ) {
                if ( array[j] > array[j+1] ) {
                    char t = array[j];
                    array[j] = array[j+1];
                    array[j+1] = t;
                }
            }
        }
    }

}

答案 2 :(得分:0)

您可以使用HashMap计算字符的频率,其中字符是键本身,并在找到该值时递增值。你当前的算法是O(n ^ 2),因为你在另一个内部有一个for,但如果你在O(n)上使用hashmap dicrease

实施例。

    String text = "Hello world";
    HashMap<Character, Integer> chars = new HashMap<>();
    for(int i = 0; i < text.length(); i++){

        //Get the current char
        char letter = text.charAt(i);

        //If the hashmap has the key is a new ocurrence
        if(chars.containsKey(letter)){
            chars.put(letter, chars.get(letter) + 1);
        }else{
            chars.put(letter, 1);
        }
    }

之后你必须对键进行分类。

    for(char tempChar : chars.keySet()){
        //Sort the current char by type
    }

你有两次O(n)时间复杂度的迭代。所以它最后是O(n)而没有O(n ^ 2)。

您当前的算法不适合大量数据。

检查这一点以了解原因。 Big-O Algorithm Complexity

问候。