Java泛型和铸造到原始类型 [英] Java generics and casting to a primitive type

查看:139
本文介绍了Java泛型和铸造到原始类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正试图学习如何从一本书中使用泛型。在本章中,它说要取一块数据T并将其转换为一个整数。我在Eclipse尝试不同的东西,但没有一个似乎允许这样。您如何执行以下任务:

  LinkedList< T& arr = new LinkedList< T>(); 

Float fl = 8.74273123948;
arr.add(fl);

然后在另一个类中:

  public int findValue(Node node)
{
T data = node.data;
int value = Number.valueOf(data);
返回值;
}



我尝试过使用 .valueOf code>和(int)之间,没有什么似乎满足Java。本书坚持在使用浮动或双精度而不是字符串或整数时保持方法的通用性。



编辑:对于可能有类似问题的其他人。从对此问题的所有意见和接受的答案中收集:



对数据使用.toString(),然后根据需要解析它,无论数据类型你需要。

解决方案

嗯,这是一本很奇怪的书。我会尝试告诉你它的基础上我的知识的要点。



泛型是一个允许你编译时检查一个类型是否正在尝试在特定的集合,方法或类中使用实际上是知道该特定的东西所需的功能。



例如,您需要使用该函数由您的模板中名为SearchParameter的接口确定,但您只能将< T> 参数视为对象。或者在你的情况下一个更好的例子是一个自定义接口 IntegerConvert 如下:

 code> public interface IntegerConvert 
{
Integer returnAsInteger();
}

你可以有这样的类:

  public class MyData implements IntegerConvert 
{
private String data;

public MyData(String data)
{
this.data = data;
}

@Override
public Integer returnAsInteger()
{
return Integer.parseInt(data); // throws ParseException如果它不工作
}
}

那么你可以有这样的列表:

 列表< IntegerConvert> listOfConvertibles = new ArrayList< IntegerConvert>(); 

或如果您想对未来更通用,

 列表< ;? extends IntegerConvert> listOfConvertibles = new ArrayList< IntegerConvert>(); 

,然后您可以执行

  listOfConvertibles.add(25); 
listOfConvertibles.add(40);
listOfConvertibles.add(35);
for(IntegerConvert ic:listOfConvertibles)
{
System.out.println(+ ic.returnAsInteger());
}

虽然这是一个过于复杂的例子,一个更简单的例子如下:

  public class Node< E> 
{
私人E数据;

public Node(E e)
{
this.data = e;
}

public E getData()
{
return data;
}

public void setData(E e)
{
data = e;
}

public void print()
{
System.out.println(data.toString());
}
}

public class MyClass
{
public void doSomething()
{
List< Node< Float& > listOfFloatNodes = new ArrayList< Node< Float>>();
listOfFloatNodes.add(new Node< Float>(new Float(8.7472742f)));
listOfFloatNodes.add(new Node< Float>(new Float(5.56842742f)));
listOfFloatNodes.add(new Node< Float>(new Float(6.5467742f)));
MyOtherClass moc = new MyOtherClass();
moc.useNodeList(listOfFloatNodes);
}
}

public class MyOtherClass
{
public< E> void useNodeList(List< Node< E>> list)
{
for(Node E node:list)
{
printNode
}
}

public< E> void printNode(Node< E> node)
{
node.print
}
}

public class MainClass
{
public static void main(String [] args)
{
MyClass myClass = new MyClass();
myClass.doSomething();
}
}

如果您有任何问题, >

I am currently trying to learn how to use Generics from a book. In this chapter it says to take a piece of data T and convert it to an integer. I am trying different things in Eclipse, but none of them seem to allow this. How could you perform the following task:

LinkedList<T> arr = new LinkedList<T>();

Float fl = 8.74273123948;
arr.add(fl);

Then in another class:

public int findValue(Node node)
{
  T data = node.data;
  int value = Number.valueOf(data);  
  return value;
}

I have tried using .valueOf() and (int) among a few other things and nothing seems to satiate Java. The book insists on keeping the method generic in case floats or doubles were used instead of strings or ints.

EDIT: For other people that might have a similar question. Gleaned from all the comments to this question and the answer that was accepted:

use the .toString() on the data and then parse it as you need to whichever data type you need.

解决方案

Hmm, that is an odd book. I'll try to tell you the gist of it based on what I know.

Generics are a construct that allow you compile-time check of whether a type you are trying to use in a specific collection, method, or class is actually something that knows the functionality that is necessary for that specific thing to function.

For example, you need to use the function determined by the interface called SearchParameter in your template, but you only see the <T> parameter as an object. Or maybe a better example in your case would be a custom interface called IntegerConvert like so:

public interface IntegerConvert
{
    Integer returnAsInteger();
}

And you could have a class like this:

public class MyData implements IntegerConvert
{
    private String data;

    public MyData(String data)
    {
        this.data = data;
    }

    @Override
    public Integer returnAsInteger()
    {
        return Integer.parseInt(data); //throws ParseException if it doesn't work
    }
}

And then you could have a List of these like this:

List<IntegerConvert> listOfConvertibles = new ArrayList<IntegerConvert>();

or if you want to go a bit more generic for the future,

List<? extends IntegerConvert> listOfConvertibles = new ArrayList<IntegerConvert>();

and then you can do

listOfConvertibles.add("25");
listOfConvertibles.add("40");
listOfConvertibles.add("35");
for(IntegerConvert ic : listOfConvertibles)
{
    System.out.println("" + ic.returnAsInteger());
}

Although that was a bit of an overcomplicated example, I guess. A simpler example would be the following:

public class Node<E>
{
    private E data;

    public Node(E e)
    {
        this.data = e;
    }

    public E getData()
    {
        return data;
    }

    public void setData(E e)
    {
        data = e;
    }

    public void print()
    {
        System.out.println(data.toString());
    }
}

public class MyClass
{
    public void doSomething()
    {
        List<Node<Float>> listOfFloatNodes = new ArrayList<Node<Float>>();
        listOfFloatNodes.add(new Node<Float>(new Float(8.7472742f)));
        listOfFloatNodes.add(new Node<Float>(new Float(5.56842742f)));
        listOfFloatNodes.add(new Node<Float>(new Float(6.5467742f)));
        MyOtherClass moc = new MyOtherClass();
        moc.useNodeList(listOfFloatNodes);
    }
}

public class MyOtherClass
{
    public <E> void useNodeList(List<Node<E>> list)
    {
         for(Node<E> node : list)
         {
             printNode(node);
         }
    }

    public <E> void printNode(Node<E> node)
    {
         node.print();
    }
}

public class MainClass
{
    public static void main(String[] args)
    {
        MyClass myClass = new MyClass();
        myClass.doSomething();
    }
}

If you have any questions, comment.

这篇关于Java泛型和铸造到原始类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆