tags:

views:

472

answers:

5
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

A: 

Use Generic collections in place you use Java arrays.

public <T> void setSRC(T var)

instead of

new int[1]

use

Collection<T> collection = new ArrayList<T>();
Mykola Golubyev
+6  A: 

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. Your code can be replaced by:

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

public <T> void setSRC(T var) {
    src.add(var);
}
jqno
I'm not sure you can define src as a generic collection like that.
Curtis Tasker
Well, I did assume that Kam was free to change the declaration of the src field, even though he did not say so in his question.
jqno
it asks me to create class T if I use the above code for generic? is it right?
Kam
You have to substitute another class for T. (For example, Integer.) Or, you have to make the entire containing class generic. See http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf for a good tutorial.
jqno
A: 

Collection collection = new ArrayList(); by doing the above step, It asks me to create class T?

please help..

Kam
Add <T> after the enclosing class name: class MyClass<T>
kd304
A: 

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. 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.

Curtis Tasker
A: 

This notation is quite confusing for Java beginners. So here's the jqno's example for Integer type values:

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. 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.

If you want to use arrays and need to change the size, then have a look at the System.arraycopy(..) method.

Andreas_D