0

I've made an array of Strings and then I've split the String.

So in the array I've [time0, operator, time1, operator, time2 ...].

Now here is where I'm stuck, in my class Time I've the method to make a new Time of an String. So I've made a loop for resorting the array.

And my idea was to make a:

Time name1 = new Time(array[i]);


Time name2 = new Time(array[i])...;

But since I don't know how many new Times I've in each new operation...

sAm
  • 319
  • 2
  • 20
Crowless
  • 11
  • 1
  • 1
    Welcome to Stack Overflow! You can take the [tour] first and learn [ask] a good question and create a [mcve]. That makes it easier for us to help you. – Katie Jan 29 '17 at 13:44
  • could you put some example. you can use `for-each` of java to iterate over your array. – jack jay Jan 29 '17 at 13:44
  • [This](http://stackoverflow.com/questions/24725374/java-postfix-calculator-push-pop-method-with-a-string-array) might give you some other ideas i.e. to use a stack for your calculator. I guess only difference being operator meanings differing than normal arithmetic operators. – Sabir Khan Jan 29 '17 at 14:07

1 Answers1

2

Yes, you can't predict a number of variables that will be needed to save time values. But you know that every second element (i, i+2, i+4) is a time value and you are free to save them into an array, or a list:

Time[] times = new Time[array.length / 2 + 1];
// List<Time> times = new ArrayList<>(array.length / 2 + 1);

for (int i =0; i < array.length; i += 2) {
    times[i] = new Time(array[i]);
    // times.add(new Time(array[i]));
}
Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142