将集合中元素的类型传递给一个初始化ArrayList的函数(Java)?

huangapple 未分类评论51阅读模式
英文:

Passing in type of elements in collection to a function which initialises an ArrayList (Java)?

问题

我想要一个函数,该函数接受一个参数,然后将 E 设置为 ArrayList 中的元素类型,其中 E 是此集合中的元素类型。因此,展示我想要实现的最简代码如下,其中 E 是我不知道的内容。

private void createArray(E dataType) {
    ArrayList<E> list = (ArrayList<E>) message.getArray();
}

这是否可能?我尝试查看 ArrayList 类以找到答案,但我对 Java 不太熟悉。

编辑:
我不确定我的问题是否足够清楚... 我不需要传递变量 dataType。我需要变量 dataType 成为我在初始化 ArrayList 时输入 <> 中的内容。我找到了以下答案:https://stackoverflow.com/questions/42069745/pass-class-type-as-parameter-to-use-in-arraylist,但对我并没有太大帮助。我需要使用数组列表中的元素并调用仅适用于该类的方法...

private <T> void createModel(String SQLcomm, Class<T> dataType, DefaultTableModel table) {
    Parcel parc = SQLtask(SQLcomm, "array");
    ArrayList<T> list = (ArrayList<T>) parc.getData();
    
    for (T t : list) {
        Vector<Object> newRow = t.getinfo();    //无法执行 t.getinfo() 吗?
        table.addRow(newRow);
    }
}
英文:

I would like a function which takes in a parameter that then sets E in the ArrayList where E is the type of elements in this collection. So the simplest code showing what I want to do is as such, where E is what I don't know.

private void createArray(E dataType) {
    ArrayList&lt;dataType&gt; list = (ArrayList&lt;dataType&gt;) message.getArray();
}

Is this at all possible? I tried to find an answer by looking at the ArrayList class but I don't have much experience with Java.

Edit:
I am not sure if my question is clear enough... I don't need to pass the variable dataType. I need the variable dataType to be what goes into <> when I do the line where I initialise the ArrayList. I have found the following answer: https://stackoverflow.com/questions/42069745/pass-class-type-as-parameter-to-use-in-arraylist but it doesn't really work for me. I need to use the elements in the arraylist and call a method which only applies to that class...

    private &lt;T&gt; void createModel(String SQLcomm, Class&lt;T&gt; dataType, DefaultTableModel table) {
    Parcel parc = SQLtask(SQLcomm, &quot;array&quot;);
    ArrayList&lt;T&gt; list = (ArrayList&lt;T&gt;) parc.getData();
    
    for (T t: list) {
        Vector&lt;Object&gt; newRow = t.getinfo();    //can&#39;t do t.getinfo()?
        table.addRow(newRow);
    }

答案1

得分: 0

一个数组不是ArrayList。但你可以使用如下方法:

如果你有一个数组,你可以使用:

Arrays.asList(array);

否则:

List<E> list = new ArrayList<>();
list.add(element);

希望这回答了你的问题。

英文:

An array isn't an ArrayList. But you can use something that method:

If you have an array you can use:

Arrays.asList(array);

Otherwise:

List&lt;E&gt; list = new ArrayList&lt;&gt;();
list.add(element);

I hope this answers your question.

答案2

得分: 0

当然是“可能的” - 你可以这样编写:

private <E> void createArray(E dataType) {
    ArrayList<E> list = (ArrayList<E>) message.getArray();
}
英文:

Sure it's "possible" - you can write:

private &lt;E&gt; void createArray(E dataType) {
    ArrayList&lt;E&gt; list = (ArrayList&lt;E&gt;) message.getArray();
}

答案3

得分: 0

你可以这样做,但是你必须将数据类型作为类参数传递,这在Java中称为泛型(有关泛型的更多信息,请参阅官方Java文档中的[链接][1])
示例:

    // E 为你的数据类型
    public class A<E> {
        // 可能从数据库获取数据(ResultSet)
        public void createArray(ResultSet message, String index) throws SQLException {
            ArrayList<E> list = (ArrayList<E>) message.getArray(index);
            // 其余代码...
        }
    }

有关处理数组的更多信息,请参阅[此处][2]的官方文档。


  [1]: https://docs.oracle.com/javase/tutorial/java/generics/types.html
  [2]: https://docs.oracle.com/javase/tutorial/jdbc/basics/array.html
英文:

You can, but you have to pass the data type as a class parameter, this is called genericity in java (for more info on genericity, here is a link to the official java doc)
Example :

// E your data type
public class A&lt;E&gt; {
    //Maybe you get data from database (ResultSet)
    public void createArray(ResultSet message, String index) throws SQLException {
        ArrayList&lt;E&gt; list = (ArrayList&lt;E&gt;) message.getArray(index);
        //the rest of the code...
    }
}

For more info on handling arrays here is the official doc.

huangapple
  • 本文由 发表于 2020年4月6日 05:36:16
  • 转载请务必保留本文链接:https://java.coder-hub.com/61049849.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定