2015-07-21 46 views
2

我想将方法​​转换为通用的CharSequence []和Set。那么我没有经验。使方法通用到CharSequence []和设置<String>

这是第二个参数/返回值应该是通用(T)的方法。可能吗?

private CharSequence[] remove(String string, CharSequence[] charSequences) 
    { 
     ArrayList<CharSequence> newArray = new ArrayList<CharSequence>(); 
     int foundPlace = -1; 
     CharSequence[] v = charSequences; 
     for (int i = 0; i < v.length; i++) { 
      if (foundPlace != -1 && v[i].equals(string)) 
       foundPlace = i; 
      else 
       newArray.add(v[i]); 
     } 
     return newArray.toArray(new CharSequence[newArray.size()]); 
    } 

我试过了。将CharSequence []出现的位置替换为T,但当我将T放入行T.length时,它不起作用。


更多解释(对不起)。我想将第二个参数和返回值转换为T - 所以到处CharSequence []到T的位置。

+0

你是否真的想要在泛型中传递CharSequence []作为此方法的第二个参数?这是不值得的努力! –

+0

您不能创建一个泛型类型的数组,以便您的return语句不起作用:return newArray.toArray(new CharSequence [newArray.size()]); –

+0

@ShishirKumar是的,给T – deadfish

回答

3

也许这是你想要什么:

private <T> T[] remove(T string, T[] charSequences) 
    { 
     ArrayList<T> newArray = new ArrayList<T>(); 
     int foundPlace = -1; 
     T[] v = charSequences; 
     for (int i = 0; i < v.length; i++) { 
      if (foundPlace != -1 && v[i].equals(string)) 
       foundPlace = i; 
      else 
       newArray.add(v[i]); 
     } 
     @SuppressWarnings("unchecked") 
     T[] ret = (T[]) Array.newInstance(v.getClass().getComponentType(), newArray.size()); 
     return newArray.toArray(ret); 
    } 

或者,如果含有类也应该是通用的T型只是从上面卸下<T>。所以这条线,

private <T> T[] remove(T string, T[] charSequences) 

然后会成为,

private T[] remove(T string, T[] charSequences) 

如果string类型并不重要,你可能会改变其类型为纯Object

private T[] remove(Object string, T[] charSequences) 
    { 
     ArrayList<T> newArray = new ArrayList<T>(); 
     int foundPlace = -1; 
     T[] v = charSequences; 
     for (int i = 0; i < v.length; i++) { 
      if (foundPlace != -1 && v[i].equals(string)) 
       foundPlace = i; 
      else 
       newArray.add(v[i]); 
     } 
     ... 
    } 

您可能还想将string重命名为其他内容。


我也应该注意到,在你的逻辑,foundPlace永远是-1

+0

@Json Sparc你不能创建一个通用数组,你的代码片段的return语句将不起作用。 http://stackoverflow.com/questions/2927391/whats-the-reason-i-cant-create-generic-array-types-in-java –

0

泛型和数组可能是一个相当痛苦的组合。我会坚持List接口,如果这是你的选择(即将返回类型更改为List并简单地返回也是List类型的newArray)。

否则我推荐这篇文章,以更好地理解泛型和数组: How to create a generic array in Java?

编辑:哦,你绝对不希望更换“的CharSequence []”与“T”,但有可能进行“T []“如果你确实需要在这里处理数组。

0

要实现一个泛型集合,使用方法:

int length; // ... 
T[] array = (T[])new Object[length]; 

因此,return语句会是这样的:

return (T[])newArray.toArray(new Object[newArray.size()]); 
1
  • 您可以使用一个整数foundPlace但布尔足够,因为你只要测试它是否已经初始化并且不使用它的值。
  • 你甚至不需要这个布尔,你的equals测试不会求,因为foundPlace将始终等于-1(所以你返回数组始终是复印件)
  • 你不需要中介变量v,你可以简单地遍历

这里是对象的一个​​版本,如果是你在找什么:

private Object[] remove(Object val, Object[] array) 
    { 
     ArrayList<Object> newArray = new ArrayList<Object>(); 
     for (Object o : array) 
      if(!o.equals(val)) 
       newArray.add(o); 
     return newArray.toArray(new Object[newArray.size()]); 
} 
0

这里是如何实现删除例程的。请注意,您甚至不需要单独的删除方法。我希望,我不会错过一个用例,需要您创建一个单独的删除方法。

package com.anupam; 

import java.util.Arrays; 
import java.util.HashSet; 
import java.util.Set; 

public class SO31535852 { 
    public static void main(String args[]) { 
    String[] arr = {"1", "2", "3"}; 

    // Create a copy of the orginal array if you do not want to modify it. 
    Set<String> test = new HashSet<>(Arrays.asList(Arrays.copyOf(arr, arr.length))); 

    // In fact you do not even need a remove method. I could not understand why you are using 
    // foundPlace variable it's not doing much. 
    System.out.println(remove("1", test)); 
    System.out.println(remove("1", test)); 
    } 

    /** 
    * Removes a specific value from the underling collection 
    * 
    * @param toRemove the object to be removed 
    * @param myCollection the colelction from which to be removed 
    * @return the removed value or null. 
    */ 
    private static <T> T remove(T toRemove, Set<T> myCollection) { 
    return myCollection.remove(toRemove) ? toRemove : null; 
    } 
} 
相关问题