一个数组的抽象?

huangapple go评论132阅读模式
英文:

An abstraction of an array?

问题

这是一个作业,其中一个任务是创建一个数组的抽象表示,这是否意味着我在这里声明了一个抽象数组?如果是这样的话,我应该如何声明它?

^^ 这里的目标是构建一个关于磁盘上整数数组的抽象表示。如果文件存在,应该打开该文件并计算其长度(忽略 size 参数)。如果文件不存在,应该创建该文件并将 size 个零写入文件。

请不要回答或给我任何代码,只回答是否可以声明该数组以及是否会起作用。

英文:

this is for a assignment, and one of the tasks is to create a abstraction of a array, does it mean that i am declaring a array that's abstract here, if so how would i declare it

private abstract Array<TYPE> Disk = new Array<TYPE>()

? something like that?
or is it impossible to create a abstract array and its talking about making a class abstract and inserting a array in that class

// Build an abstraction of an array of integers on disk. If the file exists,
* it should be opened and its length calculated (size parameter is ignored).
* If the file doesn't exist, it should be created and size zeros should be
* written to the file.
^^
goal here

please don't answer or give me any code, other than a yes or no to declaring that array and wether it would work

答案1

得分: 2

给定的输入有些稀疏,所以我只能猜测应该怎么做。

一个抽象可以有多种解释。

  • “创建一个抽象数组”并不是在构建一个抽象。通常,抽象方法用于表示该方法没有实现。在这个上下文中,我认为这毫无意义。
  • 创建抽象类也是同样道理。

或者,正如我会主张的:

  • 你必须针对某个东西创建一个抽象,这意味着你想要隐藏细节和/或信息。在你的情况下,你有一个整数数组,想要将其存储在磁盘上。此外,你有一些操作要应用于它。这就给你了一个抽象。你定义一个接口(操作),并隐藏所有其余部分(整数数组,存储在磁盘上)。换句话说,你抽象化了所有细节,并提供了一些易于使用的接口。

所以我认为,你需要创建一个包含所需操作的类,并实现一些存储/加载方法,然后返回适当的结果。

英文:

The given input is a bit sparse, so I can only guess what to do.

An abstraction can be interpreted in multiple ways.

  • To "create an abstract array" is not building an abstraction. Usually an abstract method is used to signify, that this method has no implementation. In this context I think it doesn't make any sense.
  • The same with making an abstract class.

or, as I would argue:

  • You have to create an abstraction of something, which means you want to hide details and/or information. In your case, you have an array of integers, and you want to store it on disk. Furthermore, you have some operations you want to apply to this. This gives you an abstraction. You define an interface (operations) and hide all the rest (array of ints, store on disk). In other words, you abstracted all the details, and provided some easy to use interface.

So In my opinion, you have to create a class having the asked operations, and implement some store/load methods, and return the appropriate results.

答案2

得分: 1

你的示例中不能像抽象字段那样定义抽象字段。

关于数组的抽象化,虽然你可能能够实现,但我认为不值得。List和其他类的类型参数在运行时被擦除,因此你可以对它们进行各种类型的类型转换。然而,数组知道它们元素的类,这意味着你不能仅仅根据某个类型参数 T 来写 T[] foo = new T[]{}。当然,你可以将 Object[] 强制转换为 T[],但即使在编译时这样做是可行的,但在运行时你将会得到 ClassCastException

然而,如果你除了某个类型参数 T 外还有一个 Class<T> 对象,你可以使用 Array.newInstance() 来创建数组。下面是一种你可以实现的方式,但是,将 MyArray<String> 强制转换为 MyArray<Object>,然后将 getArray 用作 Object[],会导致类转换异常。

class MyArray<T> {
  private final T[] arr;
  public MyArray(Class<T> klass, int n) {
    arr = (T[]) java.lang.reflect.Array.newInstance(klass, n);
  }
  public T get(int index) {
    return (T) arr[index];
  }
  public void set(int index, T t) {
    arr[index] = t;
  }
}
英文:

You can't have abstract fields like in your example.

About an abstraction over arrays - you might be able to do it, but I don't think it's worth it. Lists and other classes have their type parameters erased at runtime, so you can do all sorts of casting with them. However, arrays know the class of their elements, meaning you can't just do T[] foo = new T[]{} given some type parameter T. You could, of course, cast an Object[] to a T[], but even if that works at compile time, you would get a ClassCastException at runtime.

However, if you have a Class&lt;T&gt; object in addition to some type parameter T, you can use Array.newInstance() to create arrays. Here's one way you could do it, but casting, say, a MyArray&lt;String&gt; to a MyArray&lt;Object&gt;, and then using getArray as an Object[], will cause a class cast exception.

class MyArray&lt;T&gt; {
  private final T[] arr;
  public MyArray(Class&lt;T&gt; klass, int n) {
    arr = (T[]) java.lang.reflect.Array.newInstance(klass, n);
  }
  public T get(int index) {
    return (T) arr[index];
  }
  public void set(int index, T t) {
    arr[index] = t;
  }
}

huangapple
  • 本文由 发表于 2020年10月1日 06:34:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/64146658.html
匿名

发表评论

匿名网友

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

确定