java - 在构建容器时,为什么使用 Java 泛型比使用对象类更好?(Java 泛型和数据结构)
问题描述
所以我一直在审查我的数据结构,并遇到了一个关于 Java 泛型和Object
类的有趣想法。我已经以两种不同的方式实现和运行“通用包” (注意如下:IObjectBag.java、ObjectBag.java、IGenericBag.java 和 GenericBag.java)并使用了它们(注意:在 main.java 和输出下方) . 我已经根据堆栈溢出规则删除了一些不必要的代码,但如果你想要完整的实现,请告诉我。
此外,除了查看此处ArrayList
课程的源代码之外,我还在许多网站、书籍和课程中研究过该主题,我知道我的选择比我的更好,但不足以在学习期间以实用的方式解释它面试。而且我很困惑,我在其实现中使用了比我更多的转换操作(请参阅和)。GenericBag
ObjectBag
GenericBag
ObjectBag
Remove
PrintBag
那么,除了语法糖,为什么我的
GenericBag
更好呢?请以我的课程为例。我没有注意到运行时/开销/空间/时间是否有任何重要差异?
你会如何回答这个问题或期望它在面试中得到回答?
奖励问题:如果您愿意,请在评论Main
和GenericBag
评论中回答奖励问题(我想我可以自己回答,只是想听听您的意见)。
IObjectBag
界面:
public interface IObjectBag {
void add(Object item);
Object remove(Object item) throws NoSuchElementException;
boolean isEmpty();
int find(Object item);
Object get(int index);
int numItems();
}
ObjectBag
班级:
public class ObjectBag implements IObjectBag {
private Object [] items; // the java class attribute that will hold out "ints"
private int numItems;
public static void printBag(IObjectBag bag) {
for(int i = 0; i < bag.numItems(); i++) {
System.out.println(bag.get(i));
}
}
public ObjectBag(int size) {
this.items = new Object[size]; // fills array with null values
this.numItems = 0;
}
public void add(Object item){
// adds item to end of bag
}
public Object remove(Object item) {
int index = this.find(item);
if(index == -1) throw new NoSuchElementException("oops nothing found");
Object out = this.items[index];
this.items[index] = null;
this.numItems -= 1;
if(index + 1 != this.items.length && this.items[index + 1] != null) {
for(int i = index; i < this.items.length; i++) {
if(i + 1 != this.items.length) this.items[i] = this.items[i + 1];
}
this.items[this.items.length - 1] = null;
}
return out;
}
public int find(Object item) {
// return index given item or -1
}
public Object get(int index) {
// returns item given index
}
}
IGenericBag
班级:
public interface IGenericBag <T> {
void add(T item);
T remove(T item) throws NoSuchElementException;
boolean isEmpty();
int find(T item);
T get(int index);
}
GenericBag
班级:
public class GenericBag<T> implements IGenericBag<T> {
// private T[] items; can't use this b/c see comment in constructor
private Object[] items;
private int numItems;
public static void printBag(GenericBag bag) {
for(int i = 0; i < bag.numItems(); i++) {
System.out.println(bag.get(i));
}
}
public GenericBag(int size) {
// this.items = new T[size]; Bonus: throws generic array creation error (why?)
this.items = new Object[size];
this.numItems = 0;
}
public void add(T item){
this.items[this.numItems] = item;
this.numItems += 1;
}
public T remove(T item) {
int index = this.find(item);
if(index == -1) throw new NoSuchElementException("oops nothing found");
T out = (T) this.items[index];
this.items[index] = null;
this.numItems -= 1;
if(index + 1 != this.items.length && this.items[index + 1] != null) {
for(int i = index; i < this.items.length; i++) {
if(i + 1 != this.items.length) this.items[i] = this.items[i + 1];
}
this.items[this.items.length - 1] = null;
}
return out;
}
public int find(Object item) {
// given object return index or throw exception
}
public T get(int index) {
return (T) this.items[index];
}
}
Main
班级:
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.println("Hello StackOverFlow!");
Object int1 = new Integer(1);
Object int2 = new Integer(2);
Object int3 = new Integer(3);
/* using my object bag ************************************************/
System.out.println("using my object bag");
IObjectBag myObjectBag = new ObjectBag(3);
myObjectBag.add(int1);
myObjectBag.add(int2);
myObjectBag.add(int3);
myObjectBag.remove(int2);
ObjectBag.printBag(myObjectBag);
/* using my generic bag ***********************************************/
System.out.println("using generic bag");
// Bonus Question: using object like above causes error at add method (why?)
Integer int4 = new Integer(4);
Integer int5 = new Integer(5);
Integer int6 = new Integer(6);
GenericBag<Integer> myGenericBag = new GenericBag<Integer>(3);
//Bonus Question: using Interface decllaration like above causes error in print bag (why?)
myGenericBag.add(int4);
myGenericBag.add(int5);
myGenericBag.add(int6);
myGenericBag.remove(int4);
GenericBag.printBag(myGenericBag);
}
}
输出:
Hello StackOverFlow!
using my object bag
1
3
using generic bag
5
6
解决方案
比方说,使用GenericBag<String>
over的原因与在 上使用(或任何其他类型)ObjectBag
基本相同:String
Object
类型安全。
您声明某个方法返回一个字符串集合而不是其他任何东西,从而阻止您将其他对象放在那里,或者试图将您从包中得到的东西视为其他类型。当您有 100 行代码时,这听起来可能很愚蠢,但是当您使用体面的代码库时,这可能会为您节省大量调试时间。
虽然,类型安全不是灵丹妙药,它只是一种工具,有些人觉得有用,有些人觉得没用。我很确定它是任何编程论坛的热门圣战话题。
如果您觉得在没有这种范式的情况下工作很舒服(Javascript 背景,对吗?),您可能会考虑尝试一些动态类型的语言,例如 Python,而不是 Java。
推荐阅读
- postgresql - 如何将 PostgreSQL 9.6 中的数据库备份恢复到 PostgreSQL 12.2?
- c# - 如何访问 Outlook vsto 中的“收件人”自动填充电子邮件地址?缓存的联系人
- sql - T-SQL 存储过程错误:关键字“END”附近的语法不正确。严重性 15 PROCEDURE_1
- conditional-formatting - LibreOffice Calc(日期具有过去的编码格式)
- java - Solr Suggester 不适用于复制字段
- qt - QML FileDialog - 对要选择的文件设置数量限制
- javascript - connection.playStream 不是函数
- git-svn - 我可以用 subgit 映射 svn 外部吗?
- c# - 登录.net core 3.1 Web应用程序后如何在startup.cs中设置全局数据?
- sql - 使用if条件的sql多场景