简体   繁体   English

如何使它成为Java中的通用方法?

[英]how to make this a generic method in java?

public void setSRC(int var)
{
    if (src== null)
    {
        src = new int[1];
        src[0] = var;
    }
    else
    {
        int i = 0;
        int[] temp = null;
        temp = new int[src.length];
        temp = src;
        i = temp.length;
        src = new int[i+1];
        for (int j =0; j < temp.length ; j++)
                    src[j] = temp[j];
        src[i] = var;

    }
}

I am looking to make this method generic or template method. 我正在寻找使此方法通用或模板方法。 Any sort of help is appreciated. 任何帮助表示赞赏。 Looking forward to it 期待它

Use Collections, instead of arrays. 使用集合,而不是数组。 Apart from the fact that Java arrays don't mix with generics, they are actually designed to be appended to. 除了Java数组不与泛型混合使用外,它们实际上是被设计为要附加的。 Your code can be replaced by: 您的代码可以替换为:

private List<T> src = new ArrayList<T>();

public <T> void setSRC(T var) {
    src.add(var);
}

Use Generic collections in place you use Java arrays. 在使用Java数组的地方使用通用集合。

public <T> void setSRC(T var)

instead of 代替

new int[1]

use 采用

Collection<T> collection = new ArrayList<T>();

This notation is quite confusing for Java beginners. 对于Java初学者来说,这种表示法非常令人困惑。 So here's the jqno's example for Integer type values: 因此,这是整数类型值的jqno示例:

private List<Integer> src = new ArrayList<Integer>();

public void setSRC(Integer var) {
    src.add(var);
}

Now src is an ArrayList that can only store Integer type objects, and that's very close to your question. 现在src是一个ArrayList,只能存储Integer类型的对象,这与您的问题非常接近。 It can't store int values (int type is not an object) but Java 5/6 will convert int to Integer if necessary (autoboxing) so you can call setSRC with (int) i. 它不能存储int值(int类型不是对象),但是Java 5/6会在必要时将int转换为Integer(自动装箱),因此您可以使用(int)i调用setSRC。

If you want to use arrays and need to change the size, then have a look at the System.arraycopy(..) method. 如果要使用数组并需要更改大小,请查看System.arraycopy(..)方法。

This code will create a class with a generic data type and will store a dynamic array of objects of that data type. 此代码将创建具有通用数据类型的类,并将存储该数据类型的动态对象数组。

import java.util.*;
class Test2<T>
{
    private Collection<T> src = new Vector<T>();

    public void setSRC(T var)
    {
        src.add( var );
    }

    public static void main( String[] args )
    {
        Test2 t = new Test2<Integer>();
        t.setSRC( 5 );
        Test2 t2 = new Test2<Double>();
        t.setSRC( 5.5 );
    }
}

The thing is, if this is all you're doing, there's no need to bother. 问题是,如果这就是您要做的一切,那么就无需打扰。 It looks like you're trying to write your own dynamic array class, and Java has plenty of classes in the Collections Framework to choose from. 看起来您正在尝试编写自己的动态数组类,并且Java在Collections Framework中有很多类可供选择。 Why reinvent the wheel? 为什么要重新发明轮子?

Now, if you're trying to do something specific, tell us what you're trying to accomplish and we can provide a better answer. 现在,如果您要尝试做一些特定的事情,请告诉我们您要完成的事情,我们可以提供更好的答案。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM