Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 删除字符串_Java - Fatal编程技术网

Java 删除字符串

Java 删除字符串,java,Java,我有一个字符串数组,我想从该数组中删除一个特定的字符串。我该怎么做?我的代码是: private void nregexp(){ String str_nregexp = i_exp_nregexp.getText(); boolean b; for(int i=0; i<selectedLocations.length; i++){ b= selectedLocations[i].indexOf(str_nreg

我有一个字符串数组,我想从该数组中删除一个特定的字符串。我该怎么做?我的代码是:

 private void nregexp(){
        String str_nregexp = i_exp_nregexp.getText();
        boolean b;
        for(int i=0; i<selectedLocations.length; i++){
            b=  selectedLocations[i].indexOf(str_nregexp) > 0;
            if(b){
                String i_matches = selectedLocations[i];
                ........
                ........
            }
        }
    }
private void nregexp(){
字符串str_nregexp=i_exp_nregexp.getText();
布尔b;
对于(int i=0;i 0;
如果(b){
字符串i_matches=selectedLocations[i];
........
........
}
}
}

我必须从
selectedLocations

中删除
I\u匹配项
,您能给我们看一下您的代码吗?为什么不使用ArrayList,因为它支持删除(索引)和删除(对象)功能

编辑:也许

private void nregexp() {
    String str_nregexp = i_exp_nregexp.getText();
    boolean b;
    List<String> list = new ArrayList<String>(Arrays.asList(selectedLocations)); 
    for(Iterator<String> it = list.iterator(); i.hasNext();){
        String e = it.next();
        b =  e.indexOf(str_nregexp) > 0;
        // b = e.matches(str_regexp); // instead?
        if(b){
            String i_matches = s;
            it.remove(); // we don't need it anymore
            ........
            ........
        }
    }
    selectedLocations = list.toArray(new String[list.size()]);
}
private void nregexp(){
字符串str_nregexp=i_exp_nregexp.getText();
布尔b;
列表列表=新的ArrayList(Arrays.asList(selectedLocations));
for(Iterator it=list.Iterator();i.hasNext();){
字符串e=it.next();
b=e.indexOf(str_nregexp)>0;
//b=e.matches(str_regexp);//而不是?
如果(b){
字符串i_匹配=s;
it.remove();//我们不再需要它了
........
........
}
}
selectedLocations=list.toArray(新字符串[list.size()]);
}

< /代码> 您需要将数组复制到较小的数组中,省略不需要的字符串。如果这是常见的情况,则应考虑使用数组以外的其他内容,如Link键列表或ARARYLIST。

< P>我依赖于“从数组中删除特定字符串”的意思。。如果您希望删除其值,只需将其值设置为null,但是如果您的意思是实际从数组中删除该元素(您有一个包含5个元素的数组,并且希望删除该元素后的结果为4),则在不复制已删除项的数组的情况下,这是不可能的

如果需要此行为,可能需要查看动态列表,例如或

编辑:如果您想要一个简单的方法将数组复制到删除字符串的数组中,可以执行以下操作:

List<Foo> fooList = Arrays.asList(orgArray);
fooList.remove(itemToRemove);
Foo[] modifiedArray = fooList.toArray();
List-doulist=Arrays.asList(orgArray);
傻瓜。删除(项目删除);
Foo[]modifiedArray=傻瓜。toArray();

初始化数组后,无法更改数组的长度。因此,无法直接删除元素,只能将其替换为null

String[] arr = new String[10];
// fill array
...
// replace the fifth element with null
arr[4] = null;
如果要更改数组的长度,应尝试使用列表:

List<String> list = new ArrayList<String>();
// fill list
...
// remove the fifth element
list.remove(4);
List List=new ArrayList();
//填写清单
...
//拆下第五个元件
列表。删除(4);
如果你真的想自己动手,下面是一个例子:

import java.util.Arrays;
public class DelStr {
    public static String[] removeFirst(String[] array, String what) {
        int idx = -1;
        for (int i = 0; i < array.length; i++) {
            String e = array[i];
            if (e == what || (e != null && e.equals(what))) {
                idx = i;
                break;
            }
        }
        if (idx < 0) {
            return array;
        }
        String[] newarray = new String[array.length - 1];
        System.arraycopy(array, 0, newarray, 0, idx);
        System.arraycopy(array, idx + 1, newarray, idx, array.length - idx - 1);
        return newarray;
    }
    public static void main(String[] args) {
        String[] strings = { "A", "B", "C", "D" };
        System.out.printf("Before: %s%n", Arrays.toString(strings));
        System.out.printf("After: %s%n", 
            Arrays.toString(removeFirst(strings, "D")));
    }
}
导入java.util.array;
公共类DelStr{
公共静态字符串[]removeFirst(字符串[]数组,字符串内容){
intidx=-1;
for(int i=0;i
我找到了这个解决方案,它允许您删除与删除元素相等的所有元素:

private static <T> T[] removeAll(T[] array, T element) {
    if (null == array)
        throw new IllegalArgumentException("null array");
    if (null == element)
        throw new IllegalArgumentException("null element");

    T[] result = (T[]) Array.newInstance(array.getClass().getComponentType(), array.length);

    int j = 0;
    for (int i = 0; i < array.length; i++) {
        if (!element.equals(array[i]))
            result[j++] = array[i];
    }
    return Arrays.copyOf(result, j);
}
private static T[]removeAll(T[]数组,T元素){
if(null==数组)
抛出新的IllegalArgumentException(“空数组”);
if(null==元素)
抛出新的IllegalArgumentException(“null元素”);
T[]结果=(T[])Array.newInstance(Array.getClass().getComponentType(),Array.length);
int j=0;
for(int i=0;i
我还做了一些基准测试,这个解决方案肯定比使用列表要好。不过,如果性能不是问题,我会使用列表


如果您真的只需要删除一个元素(第一个元素),则有解决方案。

是否要搜索数组?您是否已经知道字符串是什么?是否希望新数组小一个元素?是否希望数组留有一个孔?等等。重复