作者:情非不得以1_810 | 来源:互联网 | 2023-05-18 17:27
Iamwritingajavaapplicationthatrequiresagenericlist.Thislistneedstobecapableofbeing
I am writing a java application that requires a generic list. This list needs to be capable of being resized dynamically and fairly often, the obvious answer for this would be a generic Linkedlist
. Unfortunately, it also needs to get/set values just as frequently as it adds/removes them via calling an index. This would be solved rather nicely by an Arraylist
. Since both of these options weren't quite what I was looking for I created my own generic array wrapper class. I have known for quite some time that it is illegal in java to create a generic array, and I have read that typically instead of using generics to define the type of your array, one would just create an object[]
array and then cast each element individually to the proper type. This is similar to what Arraylist
already does; however, I have read that casting is a very expensive operation in java. So, to get around the necessity of casting, my custom wrapper class looks something like this.
我正在编写一个需要通用列表的java应用程序。此列表需要能够动态且经常地调整大小,显而易见的答案是通用链接列表。不幸的是,它还需要像通过调用索引添加/删除它们一样频繁地获取/设置值。 Arraylist可以很好地解决这个问题。由于这两个选项都不是我想要的,所以我创建了自己的通用数组包装类。我已经知道很长一段时间在java中创建一个通用数组是非法的,我已经读过,通常不是使用泛型来定义数组的类型,只需要创建一个object []数组,然后逐个转换元素分别适当的类型。这类似于Arraylist已经做过的事情;但是,我已经读过,在java中,转换是一个非常昂贵的操作。因此,为了解决转换的必要性,我的自定义包装器类看起来像这样。
public abstract class CustomArrayWrapper{
private E[] content;
public abstract E[] empty(int n);
public CustomArrayWrapper(){
this.cOntent= empty(0);
}
public CustomArrayWrapper(int n){
this.cOntent= empty(n);
}
public CustomArrayWrapper(E[] content){
this.cOntent= content;
}
public E[] content(){
return content;
}
}
This is just the bare bones of the class, but the main idea, is that each specific use of the array wrapper extends the empty(int n)
method, returning an array of size n that is of type E
hopefully avoiding all of that expensive casting. An example is as follows using String
as the type.
这只是该类的基础,但主要思想是,数组包装器的每个特定用法都扩展了empty(int n)方法,返回大小为n的数组,类型为E,希望避免所有这些昂贵铸件。使用String作为类型的示例如下。
public class StringArrayWrapper extends CustomArrayWrapper{
public StringArrayWrapper(){
super();
}
public StringArrayWrapper(int n){
super(n);
}
public StringArrayWrapper(String[] content){
super(content);
}
public String[] empty(int n){
return new String[n];
}
}
I know that this implementation works, what I don't know is
我知道这个实现有效,我不知道的是
- Is this safe to implement?
这样安全吗?
- I know casting is a little finicky as there is a lot of implicit casting built into java, is this actually a way to get around all of the casting that an
Arraylist
already performs?
我知道转换有点挑剔,因为java内置了很多隐式转换,这实际上是一种绕过Arraylist已经执行的所有转换的方法吗?
- Is it more/less efficient than casting every element to the proper type as in an
ArrayList
?
是否比在ArrayList中将每个元素转换为正确的类型更有效/更低效?
2 个解决方案
1
I recommend you not to put effort and time trying to create your custom collection type, since standard Java API already has plenty of them. You'll win time and you'll be sure it is safe and fully functional.
For example, if you do need to resize quite often and also get and set items quite often, I would choose some of these:
我建议您不要花费精力和时间来创建自定义集合类型,因为标准Java API已经有很多。你会赢得时间,你会确信它是安全且功能齐全的。例如,如果您确实需要经常调整大小并且经常获取和设置项目,我会选择其中一些:
1- If you need more direct access than resizing: Definitely go for an ArrayList.
2- If you need more rezising than direct access: You can use a LinkedList.
1-如果您需要比调整大小更多的直接访问权限:绝对可以使用ArrayList。 2-如果您需要更多的重新启动而不是直接访问:您可以使用LinkedList。
I would try yo use a HashMap, since it is a collection that tries to optimise both direct access and resizing, so probably is the best choice for you. The only difference is that you'll have to use a key instead of an index to acces the values inside the collection, but you can still use an Integer key in the same way you an int index in an array. A good thing is you can use any kind of object as the key aswell.
我会尝试使用HashMap,因为它是一个试图优化直接访问和调整大小的集合,所以可能是你的最佳选择。唯一的区别是您必须使用键而不是索引来访问集合中的值,但您仍然可以像使用数组中的int索引一样使用Integer键。一件好事是你可以使用任何类型的对象作为关键。
Furthermore, you can use a LinkedHashMap, since it will be faster to iterate over the whole collection, if you ever need to do so.
此外,您可以使用LinkedHashMap,因为如果您需要这样做,迭代整个集合会更快。
Anyways, I can't assure you these are the best options for you to use, but I can assure you that you'll find a proper (already developed) collection type to use in the Java API.
无论如何,我无法向您保证这些是您使用的最佳选择,但我可以向您保证,您将找到在Java API中使用的正确(已开发)集合类型。
That's why I recommend you to read the official documentation on collections, you'll get a general idea about them all and be able to choose the most efficient for your particular issue --> https://www.tutorialspoint.com/java/java_collections.htm
这就是为什么我建议您阅读有关集合的官方文档,您将对它们全面了解并能够为您的特定问题选择最有效的文档 - > https://www.tutorialspoint.com/java/ java_collections.htm