简体   繁体   English

如何在不使用集合的情况下从数组中删除未使用的元素

[英]How to remove unused elements from an array without the use of the Collections

I had an array of elements that I have converted in a String. 我有一个已转换为String的元素数组。 But this String keeps again the empty elements of the array and I can't compare it with another String. 但是此String再次保留了数组的元素,因此我无法将其与另一个String进行比较。

How can I delete the empty elements on it? 如何删除上面的空白元素? I don't know how to use the Collections, is there another way? 我不知道如何使用收藏集,还有另一种方法吗?

char[] copyLoop = new char[26];
        for (int i = 0; i < letters.length; i++) {
            if (letters[i] > 1) {
                char c = (char) (i + 97);
                copyLoop[i] = c;       
            }
        }
        String rslt = String.copyValueOf(copyLoop);
        return rslt;

There are no "empty" elements in an array, and you can't resize arrays either. 数组中没有“空”元素,您也无法调整数组大小。 All you can do is to put all of the elements you're interested in at one end of the array (eg starting at zero) with no "uninteresting" elements between them, and then construct a string from a portion of the array. 您所能做的就是将您感兴趣的所有元素放在数组的一端(例如,从零开始),并且它们之间没有“无趣的”元素,然后从数组的一部分构造一个字符串。

Use another variable to store the index of the next "empty" element in the array: every time you find a letter, increment this variable. 使用另一个变量来存储数组中下一个“空”元素的索引:每次找到字母时,请递增此变量。 Once you've finished iterating letters , this variable will contain the length of the array to use to create the string. 完成letters迭代之后,此变量将包含用于创建字符串的数组的长度。

    char[] copyLoop = new char[26];
    int dst = 0;
    for (int i = 0; i < letters.length; i++) {
        if (letters[i] > 1) {
            char c = (char) (i + 97);
            copyLoop[dst++] = c;       
        }
    }
    return new String(copyLoop, 0, dst);

Or, of course, use a StringBuilder (which is doing effectively the same internally): 或者,当然,使用StringBuilder (在内部做的实际上相同):

    StringBuilder copyLoop = new StringBuilder(26);
    for (int i = 0; i < letters.length; i++) {
        if (letters[i] > 1) {
            char c = (char) (i + 97);
            copyLoop.append(c);       
        }
    }
    return copyLoop.toString();

Unfortunately not. 不幸的是没有。 Arrays in Java are not shrinkable/expandable, their size is set constant. Java中的数组不可收缩/不可扩展,其大小设置为常量。 There are two ways you can work around: 有两种解决方法:

  1. Use Collections, ie create a List of Characters ArrayList<Character> list = new ArrayList<>(); 使用集合,即创建ArrayList<Character> list = new ArrayList<>(); , use it and delete items you don't need after each loop cycle. ,使用它并在每个循环周期结束后删除不需要的项目。

  2. On every cycle of your for loop, when you need to delete an item inside your Array, just create a new char[] array, which is smaller and copy the contents of the old one to the new, except for the value you deleted. 在for循环的每个循环中,当您需要删除Array中的一项时,只需创建一个新的char []数组即可,该数组较小,并将旧值的内容复制到新值中,删除的值除外。

Also, if you are working with Strings, it's recommended to use mutable String classes sa StringBuilder or StringBuffer which let you add/remove characters from the String. 另外,如果您使用的是Strings,建议使用可变的String类,如StringBuilder或StringBuffer,这些类可让您从String中添加/删除字符。 In your case that seems to be the best solution. 在您的情况下,这似乎是最好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Java数组中“删除”对象(不能使用Collections) - How to 'remove' an Object from a Java array (can't use Collections) 如何在不使用 Arrays、Collections、Set 或 Map 类的情况下从数组中删除? - How to remove from an array without using the classes Arrays, Collections, Set, or Map? 如何在 java 中同时添加和删除 collections 中的元素? - how to add and remove elements in collections simultaneously in java? 使用地图和过滤器功能从树图中存在的集合中删除元素(Java集合) - Use map and filter functions to remove elements from a set which exists in a treemap (Java Collections) 如何从石英表中删除未使用的触发器 - how to remove unused triggers from quartz tables 如何以编程方式从 memory 中删除未使用的图像 - How to programmatically remove unused images from memory 两个如何在不使用 collections.swap 方法的情况下交换数组列表中的两个元素 - How two swap two elements in a array list without using collections.swap method 如何在不使用方法或函数的情况下删除数组的元素 - How do I remove elements of an array without using methods or functions 如何在Mongo中使用Java添加和删除数组中的元素? - How to add and remove elements in array in Mongo use Java? 如何从phonegap android插件返回数组或其他集合元素类型 - How to return a array or other collections elements type from phonegap android plugin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM