-1

I am stuck in the following problem from a long time -

I want to create a String array StrTime from 3 Int arrays.

Input Arrays

int Time1[]={"1.1","2.1","4.1"};
int Time2[]={"2.5","4.7","5.1"};
int Time3[]={"12.55","1.11","2.1"};

CurrentTime = 2.0;

So loop though each integer array, check Time1, the first array item which is greater than 2.0 should be put in our String array, Noe check Time2 for the same and Time3.

At end we should get output String array as -

Output Array

String strTime[] ={"2.1","None","2.1"}

Can someone help me????

UPDATE:

Also is there a way to create a double [] like below-\ {1,2,,3,4,,5}

4
  • 3
    that should not compile. You are assigning int[] a String[]!!! Commented Aug 11, 2012 at 13:13
  • The array initialization won't compile and - shouldn't the output be {"2.1", "2.5", "12.55"} if I get your requirements right... Commented Aug 11, 2012 at 13:14
  • @Nishant. Yes, because of that fact, I would also call it DIFFICULT Commented Aug 11, 2012 at 13:14
  • @AlexanderPogrebnyak lol Commented Aug 11, 2012 at 13:15

3 Answers 3

1

When you get it compiling, you could use org.apache.commons.collections.CollectionUtils and org.apache.commons.collection.Predicate to filter without loops.

CollectionUtils.find( getACollectionOfYourArray() , new Predicate() {
            public boolean evaluate(Object val) {
                // if is gt 2
                return true;
            }

        });
1
  • Hehe. This answer does make sense. Commented Oct 1, 2014 at 17:05
0

May be a typo, but that's the way to init the arrays (they should be double):

double time1[]={1.1, 2.1, 4.1};
double time2[]={2.5, 4.7, 5.1};
double time3[]={12.55, 1.11, 2.1};

Hints:

  • create a new String array for the result
  • for-loop through the first array
  • compare the current value with your threshold, if the current value is greater then store it in the first slot of the string array (need to convert it to string), and break out of the loop.
  • repeat the last to steps for the other two arrays
3
  • How to convert that to String. Commented Aug 11, 2012 at 14:08
  • String intAsString = "" + intValue; - this is the simplest solution for this problem. Commented Aug 11, 2012 at 14:23
  • BTW - why did you add that update to your question? The answer to that is in my and in Reimus' answers.. Haven't you read it? Commented Aug 11, 2012 at 14:24
0

Use:

    double time1[]={1.1, 2.1, 4.1};
    double time2[]={2.5, 4.7, 5.1};
    double time3[]={12.55, 1.11, 2.1};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.