使用foreach循环将值放入数组中

时间:2014-03-05 15:05:42

标签: java arrays foreach casting ascii

我有这个方法接受一个字符串,计算“不可见的”ASCII字符(意味着肉眼看不见,例如空格),并返回这些字符的总数。

到目前为止,我有:

  • 将String输入转换为char数组
  • 使用foreach

    将数组的每个char转换为int
    public static int getInvisibleCharactersCount(String inputWords) {
        int invisibleCharacters = 0;
        int decimalEquivalent = 0;
    
        char[] caInputWords = inputWords.toCharArray(); // Convert String input to char array
    
        for(char asciiChar: caInputWords) {
            decimalEquivalent = (int) asciiChar; // Cast each char of the array to int
    
            // Put ints into an intArray
        }
    
        for(int ctr = 0; ctr < intArray.length; ctr++) {
            if((decimalEquivalent >= 0 && decimalEquivalent < 33) ||
            (decimalEquivalent >= 127 && decimalEquivalent < 161)) {
                invisibleCharacters++;
            }
        }
    
        return invisibleCharacters;
    }
    

    如何将foreach的铸造内注放入intArray?

6 个答案:

答案 0 :(得分:2)

您不必将String拆分为字符数组。您可以直接使用for循环:

for (int i = 0; i < inputWords.length(); i++) {
    char c = inputWords.charAt(i);
    // do some struff with c
}

答案 1 :(得分:1)

您可以使用ArrayList动态调整大小

char[] caInputWords = inputWords.toCharArray(); // Convert String input to char array
ArrayList<Integer> intArray = new ArrayList<Integer>();
for(char asciiChar: caInputWords) {
    decimalEquivalent = (int) asciiChar; // Cast each char of the array to int

    // Put ints into an intArray
    intArray.add(decimalEquivalent);
}

或者用于reqular数组

char[] caInputWords = inputWords.toCharArray(); // Convert String input to char array
int[] intArray = new int[caInputWords.length];
for (int i = 0; i < caInputWords.length; i++) {
    decimalEquivalent = (int) caInputWords[i]; // Cast each char of the array to int

    // Put ints into an intArray
    intArray[i] = decimalEquivalent;
}

for(int ctr = 0; ctr < intArray.length; ctr++) {
    if((intArray[ctr] >= 0 && intArray[ctr] < 33) ||
      (intArray[ctr] >= 127 && intArray[ctr] < 161)) {
         invisibleCharacters++;
    }
}

答案 2 :(得分:1)

我会使用ArrayList,但因为你特别关注拥有一个Int数组。您可以创建一个大小为caInputWords的int数组,并在foreach循环内添加元素。检查下面的代码

 public static int getInvisibleCharactersCount(String inputWords) {
    int invisibleCharacters = 0;
    int decimalEquivalent = 0;

    char[] caInputWords = inputWords.toCharArray(); // Convert String input to char array
    int[] intArray = new int[caInputWords.length]; //<<-Create an int array of size of your char array
    int i= 0; //intializing your counter
    for(char asciiChar: caInputWords) {
        decimalEquivalent = (int) asciiChar; // Cast each char of the array to int

        // Put ints into an intArray
        intArray[i] = decimalEquivalent; 
        i++; //increment counter after each iteration


    }

    for(int ctr = 0; ctr < intArray.length; ctr++) {
        if((intArray[ctr] >= 0 && intArray[ctr] < 33) ||
        (intArray[ctr] >= 127 && intArray[ctr] < 161)) {
            invisibleCharacters++;
        }
    }

    return invisibleCharacters;
}

答案 3 :(得分:0)

设置一个集合,例如ArrayList,然后添加到该集合。如果最后您希望转换为数组,则可以执行以下操作:

int[] intArray = intArrayList.toArray();

答案 4 :(得分:0)

由于您之前不知道i invisible个字符的数量,因此最好创建列表。我更喜欢迭代列表,但如果你想要进一步处理数组,下面是示例代码。

public static int getInvisibleCharactersCount(String inputWords){         int invisibleCharacters = 0;         int decimalEquivalent = 0;

    char[] caInputWords = inputWords.toCharArray(); // Convert String input to char array
    List<Integer> intList = new ArrayList<Integer>();
    Integer[] intArray =  null;

    for(char asciiChar: caInputWords) {
        decimalEquivalent = (int) asciiChar; // Cast each char of the array to int

        intList.add(decimalEquivalent );
    }

   intArray = new Integer[intList.size()]

   intArray = intList.toArray(intArray);

    for(int ctr = 0; ctr < intArray.length; ctr++) {
        if((decimalEquivalent >= 0 && decimalEquivalent < 33) ||
        (decimalEquivalent >= 127 && decimalEquivalent < 161)) {
            invisibleCharacters++;
        }
    }

    return invisibleCharacters;
}

答案 5 :(得分:0)

从assylias,说char已经是一个int,我意识到下面的代码产生相同的结果,而不需要强制转换或创建另一个整数数组:

public static int getInvisibleCharactersCount(String inputWords) {
        int invisibleCharacters = 0;

        char[] caInputWords = inputWords.toCharArray();

        for(int ctr = 0; ctr < caInputWords.length; ctr++) {
            if((caInputWords[ctr] >= 0 && caInputWords[ctr] < 33) ||
              (caInputWords[ctr] >= 127 && caInputWords[ctr] < 161)) {
                 invisibleCharacters++;
            }
        }

    return invisibleCharacters;
}