热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

自定义数组包装类铸造安全/效率-Customarraywrapperclasscastingsafety/efficiency

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

我知道这个实现有效,我不知道的是

  1. Is this safe to implement?
  2. 这样安全吗?

  3. 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?
  4. 我知道转换有点挑剔,因为java内置了很多隐式转换,这实际上是一种绕过Arraylist已经执行的所有转换的方法吗?

  5. Is it more/less efficient than casting every element to the proper type as in an ArrayList?
  6. 是否比在ArrayList中将每个元素转换为正确的类型更有效/更低效?

2 个解决方案

#1


1  

Check unrolled linked list, this might be what you need.

检查展开的链表,这可能就是您所需要的。

Next, you might want to consider actually implementing a List by extending AbstractList to stay withing the collection framework. An "array wrapper" will end up cumbersome to work with.

接下来,您可能需要考虑通过扩展AbstractList来实际实现List以保持集合框架。 “数组包装器”最终会使用起来很麻烦。

Next, casting may have some overhead (see Does Java casting introduce overhead? Why?) but I don't think it's anything worth considering. The downside of your approach is that you'll need to inmplement the empty method on each use. It's a price too high for questionable performance improvements from my point of view.

接下来,转换可能会有一些开销(请参阅Java转换是否会引入开销?为什么?)但我认为这不值得考虑。你的方法的缺点是你需要在每次使用时实现空方法。从我的角度来看,这对于可疑的性能改进而言价格太高了。

To answer your questions:

回答你的问题:

  • Creating array template method implemented in subclass is OK. What's less safe is that you expose this array in the content method.
  • 创建子类中实现的数组模板方法是可以的。不太安全的是你在content方法中公开这个数组。

  • I don't think you can get rid of casting. I also don't think casting performance is something to worry about.
  • 我不认为你可以摆脱铸造。我也不认为铸造性能是值得担心的。

  • Run benchmarks and measure it. I think it will be a little bit more efficient since type checks will not be needed, but this will not be something significant/noticeable.
  • 运行基准并测量它。我认为它会更有效率,因为不需要类型检查,但这不会是重要/明显的。

#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


推荐阅读
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 开发笔记:实验7的文件读写操作
    本文介绍了使用C++的ofstream和ifstream类进行文件读写操作的方法,包括创建文件、写入文件和读取文件的过程。同时还介绍了如何判断文件是否成功打开和关闭文件的方法。通过本文的学习,读者可以了解如何在C++中进行文件读写操作。 ... [详细]
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了C#中生成随机数的三种方法,并分析了其中存在的问题。首先介绍了使用Random类生成随机数的默认方法,但在高并发情况下可能会出现重复的情况。接着通过循环生成了一系列随机数,进一步突显了这个问题。文章指出,随机数生成在任何编程语言中都是必备的功能,但Random类生成的随机数并不可靠。最后,提出了需要寻找其他可靠的随机数生成方法的建议。 ... [详细]
  • 本文介绍了在CentOS上安装Python2.7.2的详细步骤,包括下载、解压、编译和安装等操作。同时提供了一些注意事项,以及测试安装是否成功的方法。 ... [详细]
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
author-avatar
情非不得以1_810
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有