返回与数组中的连续序列匹配的第一类型的索引

时间:2013-06-24 20:11:50

标签: java algorithm

我的对象如下所示:请注意,这不是家庭作业,而是我自己做的网络项目。所以帮助将不胜感激!

enum size { small, big; }

Class Controller
{
    size sizeType;
}

Class square extends shape
{
    int num = 1;
}

Class circle extends shape
{
  int num=2;
  size size = size.Small;
}

void method()
{
    Controller[] sizes = new Controller[n];
    // e.g. shape = {small, big, small, small, big}

    Shape circle = new Shape();
    /* Find 2 'small' continuous circles 
       OR find 'size' based on the num value 
       (circle has 2 but should be able to accept 
       any integer = num declared in the shape class */

    // RETURN occurrence of first such index for e.g. 2 as found in 2,3    
}

1 个答案:

答案 0 :(得分:1)

你的代码存在真正的问题:你完全破坏了对象的层次结构。

首先,根据我对你的意图的猜测,这里有一些小的修正:

enum SizeType { SMALL, BIG; }

class Shape {
    SizeType size;
}

class Square extends Shape {
    int num = 1;
}

class Circle extends Shape {
    int num = 2;
    SizeType size = SizeType.SMALL;
}

int findShapeSequence(Shape[] shapes) {
    // TODO find the first instance of a repeated size value in the array,
    // and return the index of the first of the shapes whose size repeats.   
}

现在已经清除了,你应该很容易弄清楚如何实际做你想做的方法。