2013-06-24 38 views
-3

我的对象如下所示:请注意,这不是家庭作业,而是我自己正在做的Web项目。所以帮助将不胜感激!返回与数组中连续序列相匹配的第一类索引

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  
} 
+5

什么问题? – egrunin

+0

你的“Shape”类在哪里? – AJMansfield

+0

“type”枚举在哪里? – AJMansfield

回答

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. 
} 

现在,这个被清除,它应该很容易为你找出如何实际上做你想要的方法。

+0

如何找到2个类型为小圆的连续形状序列? – Phoenix

+0

@Phoenix这是你的工作,搞清楚。我会帮助你,但只有当你真正尝试自己解决它时才会帮助你。提示:使用'for'循环遍历形状,并将当前'Shape'的'size'值与下一个值的'size'值进行比较。 – AJMansfield

+0

但形状的数量可以是1/2/3,但不是固定的。对于例如如果有5 ..有一个更简单的方法来做到这一点? – Phoenix

相关问题