当前位置: 技术问答>java相关
测试一下这里是否有OO高手, 这是我贴在买买提bbs的一个小挑战的第一步, 没人能够给出令人满意的回答.
来源: 互联网 发布时间:2015-05-23
本文导语: public interface Array { public Object get(int i); public void set(int i, Object val); public int getLength(); } public class ArrayUtil { public void sort(Array arr, Comparator c){......} } Q1. do yo know how can you make th...
public interface Array
{
public Object get(int i);
public void set(int i, Object val);
public int getLength();
}
public class ArrayUtil
{
public void sort(Array arr, Comparator c){......}
}
Q1. do yo know how can you make the ArrayUtil.sort method work for any array like objects like String[], int[], java.util.Vector etc?
Q2. I'd like to implement array slicing, rotating, reversing, concatenating in CONSTANT TIME, how do you implement it? (after these operations, the Array instances should still point to the same underlying array container)
换句话说, arr.set(1, obj) 应该和 arr.slice(1, 2).set(0, obj) 有一样作用.
Array Slicing:
slice array [1, 2, 3, 4] with slice(1, 2) will end up with [2, 3]
Array rotating:
rotate array [1, 2, 3, 4] with rotate(2) will end up with [3,4,1,2]
Array reversing:
reverse array [1, 2, 3, 4] will end up with [4, 3, 2, 1]
Array concatenating:
cocat array [1, 2] with [3, 4] will end up with [1, 2, 3, 4]
别告诉我"用jdk ".
{
public Object get(int i);
public void set(int i, Object val);
public int getLength();
}
public class ArrayUtil
{
public void sort(Array arr, Comparator c){......}
}
Q1. do yo know how can you make the ArrayUtil.sort method work for any array like objects like String[], int[], java.util.Vector etc?
Q2. I'd like to implement array slicing, rotating, reversing, concatenating in CONSTANT TIME, how do you implement it? (after these operations, the Array instances should still point to the same underlying array container)
换句话说, arr.set(1, obj) 应该和 arr.slice(1, 2).set(0, obj) 有一样作用.
Array Slicing:
slice array [1, 2, 3, 4] with slice(1, 2) will end up with [2, 3]
Array rotating:
rotate array [1, 2, 3, 4] with rotate(2) will end up with [3,4,1,2]
Array reversing:
reverse array [1, 2, 3, 4] will end up with [4, 3, 2, 1]
Array concatenating:
cocat array [1, 2] with [3, 4] will end up with [1, 2, 3, 4]
别告诉我"用jdk ".
|
I guess nobody answered Q2 because your hint already said it all. You want to use a level of indirection on array indices, and it's easy to do so by introducing new objects implementing the Array interface and perform the required indirection on array indices in get/set/length methods. For example, ReversedArray will have a get method returning element at getLength()-i-1 instead of i, the same goes for set method. When reverse is called, just return a new ReverseArray object with the same underlying java array.
BTW, I wonder why you used reflection in your JArray2Array class, if you let adapt method take an Object[] instead of Object, you won't need to use reflection (which is slow) and you can avoid the problem of people passing in an object which is not an array.
BTW, I wonder why you used reflection in your JArray2Array class, if you let adapt method take an Object[] instead of Object, you won't need to use reflection (which is slow) and you can avoid the problem of people passing in an object which is not an array.
|
public class ReversedArray
extends AbstractFunctionalArray
{
//~ Methods ...............................................................
protected int getIndex( int i )
{
return this.getLength() - i;
}
}
extends AbstractFunctionalArray
{
//~ Methods ...............................................................
protected int getIndex( int i )
{
return this.getLength() - i;
}
}
|
What do you need String[] to implement Comparator, it doesn't make sense, Comparators compare things, they are not containers. In order to pass String[] to your ArrayUtil instance method, you could build a new class StringArray which implements Array interface and takes String[] as constructor argument. Now since your "Array" doesn't have type information, you can probably get away with an ArrayArray which takes Object[] in constructor.
Anyway, what is the point of this exercise? Collection framework's List does what your Array do.
Anyway, what is the point of this exercise? Collection framework's List does what your Array do.
|
呵呵,都怪我,闹得大家都不高兴,罚自己做做“体力活”,还请多多指教
public class AbstractFunctionalArray
implements Array
{
//~ Instance/static variables .............................................
protected Array _arr;
//~ Constructors ..........................................................
public AbstractFunctionalArray( Array arr )
{
this._arr = arr;
}
//~ Methods ...............................................................
public int getLength()
{
return _arr.getLength();
}
public Object get( int i )
{
return _arr.get( getIndex( i ) );
}
public void set( int i, Object val )
{
_arr.set( getIndex( i ), val );
}
protected int getIndex( int i )
{
return i;
}
}
public class AbstractFunctionalArray
implements Array
{
//~ Instance/static variables .............................................
protected Array _arr;
//~ Constructors ..........................................................
public AbstractFunctionalArray( Array arr )
{
this._arr = arr;
}
//~ Methods ...............................................................
public int getLength()
{
return _arr.getLength();
}
public Object get( int i )
{
return _arr.get( getIndex( i ) );
}
public void set( int i, Object val )
{
_arr.set( getIndex( i ), val );
}
protected int getIndex( int i )
{
return i;
}
}
|
呵呵,ajoo老哥在这儿呢。
针对四个操作函数写四个包装类,每个类的接口与array相同,但内部组织方式则与相应的操作相关,如何?
针对四个操作函数写四个包装类,每个类的接口与array相同,但内部组织方式则与相应的操作相关,如何?