应该把Java的数组作为对象吗? [英] Should Java treat arrays as objects?

查看:96
本文介绍了应该把Java的数组作为对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我常常想,这将是一个好主意,允许使用数组作为自己的方法,而不是依靠辅助类如数组,数组和ArrayUtils合适的对象。

例如:

  ints.sort(); // Arrays.sort(整型);
INT [] onemore = ints.add(8); // INT [] = onemore ArrayUtils.add(整型,8);

我相信我不是第一个使用这种想法,但我有麻烦寻找之前谁写了关于这个人。谁能帮我对这个话题提供一些参考?

这是认为是好还是坏主意,为什么?

如何容易会这样来实现?

一些其他的例子可能包括(但不要让他们挂了,他们是无关的问题本身):

  INT [] =整数{5,4,3,2,1};// Arrays.sort(整型);
ints.sort();// INT POS = Arrays.asList(整型).indexOf(5);
// INT POS = ArraysUtils.indexOf(整数,5);
INT POS = ints.indexOf(5);// Arrays.reverse(整型);
ints.reverse();阵列<整数GT;阵列=整数; //投地的超类。// INT长度= Array.getLength(数组);
INT长度= array.getLength();//对象N = Array.get(排列3);
对象N = array.get(3);// Array.set(数组,3,7);
array.set(3,7);obj对象数组=;
//如果(OBJ的instanceof INT [])
//的System.out.println(与Array.toString((INT [])OBJ));
//如果别人(....)
的System.out.println(OBJ);


解决方案

数组不是Java类中一个很好的理由 - 它们映射到好人们如何数组应该从C风格的编程语言经验的工作的了解。也有用于制作阵列低级别的容器,而不是对象性能的原因。正因为如此,有时也有使用基本数组,而不是一个集合的性能优势。

如果你想使用的对象,你应该只使用一个集合(一个ArrayList是一个集合的一个例子)。它可以是笨重的,但集合提供您似乎想的不错方法访问的类型。

I have often thought it would be a good idea to allow the use of arrays as proper objects with their own methods instead of relying on helper classes like Arrays, Arrays and ArrayUtils.

For example:

ints.sort();                 // Arrays.sort(ints);
int[] onemore = ints.add(8); // int[] onemore = ArrayUtils.add(ints, 8);

I am sure I am not the first with this idea but I have had trouble searching for others who have written about this before. Can anyone help me with some references on this topic?

Is this thought to be a good or bad idea, and why?

How easy would this be to implement?

Some other examples might include (but don't get hung up on them, they're extraneous to the question itself):

int[] ints = {5,4,3,2,1};

// Arrays.sort (ints);
ints.sort();

// int pos = Arrays.asList(ints).indexOf (5);
// int pos = ArraysUtils.indexOf (ints, 5);
int pos = ints.indexOf (5);

// Arrays.reverse (ints);
ints.reverse();

Array<Integer> array = ints; // cast to super class.

// int length = Array.getLength (array);
int length = array.getLength();

// Object n = Array.get (array, 3);
Object n = array.get (3);

// Array.set (array, 3, 7);
array.set (3, 7);

Object obj = array;
// if (obj instanceof int[])
//     System.out.println(Array.toString((int[]) obj));
// else if (....)
System.out.println (obj);

解决方案

Arrays are not classes in Java for a good reason - they map well onto people's understanding of how an array should work from experience with C-style languages. There are also performance reasons for making arrays low-level containers rather than objects. Because of this, sometimes there are performance benefits to using a primitive array rather than a Collection.

If you want to use objects, you should just use a Collection (an ArrayList is an example of a collection). It can be clunky, but Collections provide the type of nice methodological access that you seem to want.

这篇关于应该把Java的数组作为对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆