问题1 可以被解释为: 给你String[ ], int[ ] 或 java.util.Vector事例, 你怎么使用ArrayUtil.sort 来排序?

解决方案 »

  1.   

    Why do you call it 体力活?
    You really think this will need much coding?
    As I said, we need CONSTANT-TIME operations. So the we can't use the way JDK is doing it.This is a kind of question that 
        if you know OO design well, several sentences would clarify everything
        if you don't know OO design, you might think this is a hard thingIn short, this is a very simple question, it's just the first step of my challenge,
    if you cannot pass this step, there's no way you can even understand my second step.I don't need your full code, just your idea (like how you are going to deal with it, what kind of pattern you are going to use).
      

  2.   

    On mit bbs, somebody was giving a partially correct answer for this step, although he could not give good solution for my second step.
      

  3.   

    难者不会, 会者不难.
    提示:
    这实际上只是two patterns.要不要我给解法啊?
      

  4.   

    I'll not evenly distribute the 100 points. Whoever gives the right solution (don't need your code, just show me how you are going to do it), the 100 points are yours.
    then we can proceed to the second step (much harder!)
      

  5.   

    And if you can give straight good answer for even the second step in the first place (I don't think that's possible), I'll donate all the points I have to you. 
    It's hard to find someone who really understand OO.
      

  6.   

    Q1: force those "objects " implement the "Comparator" interface.
     am i correct ?
      

  7.   

    yes, they have to have Comparator impl. but for String, you don't have to implement it yourself. String.CASE_INSENSITIVE_ORDER is already available.Think about sort(Array, Comparator),
    how you can pass a String[] instance in?
    ArrayUtil.sort({"1", "3", "2"}, String.CASE_INSENSITIVE_ORDER);
    ?
    That won't compile.
      

  8.   

    And how you can force String[] to implement Comparator? There might be 100 different kind of order definition.
      

  9.   

    Hint for Q2: 下标变换举个例子, 我有一个Array arr, 代表{1, 2, 3, 4}, reverse(arr)并不是真正去把整个数组的内容翻过来, 那就不是CONSTANT TIME啦。 而且我们要求arr
    在调用reverse(arr)后保持不变, 仍然是{1,2,3,4}。只是reverse(arr)的返回值代表{4, 3, 2, 1}就像Array arr2 = reverse(arr); 这时当调用arr2.get(0)时,它只是把0变换成3而已。而且当调用arr2.set(0, 5)之后,arr应该是{1, 2, 3, 5}, arr2应该是{5, 3, 2, 1}
      

  10.   

    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.
      

  11.   

    Good! Congr.
    you've got the Q1.
    it's just a simple adapter pattern.
    The point of Q1 is to justify the usage of interface Array. With this interface, we write all array based algorithms (like the sort) based on it, and user can write their own adapters for different types to use these algorithms.
    And for Q1, java.util.Arrays can be used to build a generic adapter for all Something[].By the way, I don't think collection framework does the same thing. It's hard to write an adapter to adapt an array to java.util.List because List has too many irrelevant methods (I don't like collection framework, they do have many functionalities, but the interfaces and the structure are ugly). 
    And I already said, this is a challenge, whether jdk already has this is not important.
      

  12.   

    For those who don't know what adapter pattern is, the following code is essentially an adapter.public class JArray2Array implements Array
    {   
        public int getLength()
        {return java.lang.reflect.Array.getLength(arr);}
        public void set(int i, Object val)
        {java.lang.reflect.Array.set(arr, i, val);}
        public Object get(int i)
        {return java.lang.reflect.Array.get(arr, i);}
        
        private final Object arr;
        private JArray2Array(Object arr){this.arr = arr;}        
        public static Array adapt(Object arr)
        {
                return new JArray2Array(arr);
        }}then if you have an int[] x={2, 1, 3};
    you can:
    ArrayUtil.sort(JArray2Array.adapt(x), 
        new Comparator(){
            public int compareTo(Object other)
            {
                int compare(Object o1, Object o2)
                {
                    return ((Integer)o1).compareTo(o2);
                } 
            }               
        });
    And x will become {1, 2, 3}
      

  13.   

    Well, the good thing about collection framework is you don't need to write them yourself. Anyway, most people come here looking for help on their projects, so it's a bit weired when you use a title like above.
      

  14.   

    Having seen so many people talking about OO, Refactoring, XP blah blah blah in csdn, I think a more practical example would be helpful.Hope Q1 can help show what adapter pattern is. My point is to discuss some way of OO design.Still nobody knows how to do Q2, anyway?
      

  15.   

    Come on! as you guys said, "这么简单的问题". I can't believe nobody knows.
      

  16.   

    To MrYoucheng:I'm sure you will give the very naive solution, which anybody can do, and that's not what I want.I post this to incur some discussion of OO design. But seems even nobody feel interested or in it or even understand it. Maybe to you guys, OO only means a syntax of "object.method()".I'll keep this post for one more day. If nobody knows, I'll post the answer (who knows whether you can understand). And that's it. I thought csdn might have some OO guru (if anybody can pass my second step, he's certainly an OO expert)So disappointed!
      

  17.   

    He he. Sorry sorry. :-)
    did not want to show off anything. Don't want to offend anybody.As I said, just wanted to incur some discussion on OO design. 
    I'm fine if nobody is interested in it.
    But doesn't calling me as "posting homework" hurt me also?
    Is "好无聊……" very politeful?
      

  18.   

    Sorry,我知道,我也说了“体力活”,是我太浅薄,如果由此引起你的反感的话,我道歉:)
    确实是我自己太浅薄,你的反射用得很巧妙,以后有问题请你多指教,不知可否:)
      

  19.   

    I admit, I used some provoking words also in the hope that someone will stand up and beat me on this question. "See? your question is so simple, I can solve it in one minute! Don't talk as if you were a guru!".
    Really sorry if those words hurt you guys.JDK has thousands classes, I only know very limited part of it. 
    We can discuss together.
      

  20.   

    呵呵,大家都别做批评与自我批评了吧
    或许,大家都有错;也有可能大家都没错,不必去计较这些,转入正题吧
    Q2是用Decorator实现吗?
      

  21.   

    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.
      

  22.   

    呵呵,都怪我,闹得大家都不高兴,罚自己做做“体力活”,还请多多指教
    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;
        }
    }
      

  23.   

    public class ReversedArray
        extends AbstractFunctionalArray
    {
        //~ Methods ...............................................................    protected int getIndex( int i )    {
            return this.getLength() - i;
        }
    }
      

  24.   

    看起来象道算法题。question 2 confused me. 没说明白。
    如果按照理解,对于某一指定的数组,要求这四种运算必须经过的计算次数要完全一样,这样做起来头痛。
    不要说用OBJECT类型的和PRIMARY类型的计算时间是不同的,即使相同,也是很难做。对于奇数个数的数组和偶数个数的数组也有不同的考虑。分离和合并两个运算还好办一些。
    你们教授真狠!!!比我们的还狠!
      

  25.   

    呵呵,ajoo老哥在这儿呢。
    针对四个操作函数写四个包装类,每个类的接口与array相同,但内部组织方式则与相应的操作相关,如何?
      

  26.   

    不就是java中的GP编程问题嘛???不用担心的,不过你要是用C++就好了。不过话又说回来,JAVA中很快就可以看到
    对GP支持的版本了。
      

  27.   

    JGL(Java Generic Library)不是早就出了吗?有兴趣可以去看看,在sun的网站也有介绍,不少客户呼吁sun用jgl替换jdk里的容器。