0

I am new to java, and we are learning arrays in class. I have a problem from my CS textbook that I have been working on. I know generally what I am supposed to do, but I do not know if the syntax for what I would like to add to the code fragment would be syntactically proper. The textbook sounds like it wants me to add code on the next line; instead, I would have modified the second declaration and assigned the length to it on the same line. I have found a few other similar questions on here, but since I am new to this, I dont really understand those answers, and would like to know specifically why this one is the way it is :)

This is the book question:

What needs to be added to the following code fragment so that all values except the first two (100000.0 and 110000.0) are copied from allSalaries to workerSalaries?

Here is the code fragment I am given:

double[] allSalaries = {100000.0, 110000.0, 25000.0, 18000.0, 30000.0, 9000.0, 12000.0};
double[] workerSalaries;

Would it still be syntactically proper if I did on the next line:

workerSalaries = new double[allSalaries.length - 2];

Or would I replace the second line of the fragment with this:

double[] workerSalaries = new double[allSalaries.length - 2];

Thanks!!

  • I don't understand the answers for that question that you linked me to @Tunaki – IronMaiden28 Feb 09 '16 at 22:42
  • What don't you understand? They all point to how declare an array in Java. – Tunaki Feb 09 '16 at 22:43
  • Wait, I think I understand now. The answer is that for this to be correct workerSalaries = new double[allSalaries.length - 2]; I would have to change it to workerSalaries[] = new double[allSalaries.length - 2]; right? @Tunaki – IronMaiden28 Feb 09 '16 at 22:46
  • The declaration is either `double[] workerSalaries = new double[length]` or `double workerSalaries[] = new double[length]`. It's actually the same, both work. – Tunaki Feb 09 '16 at 22:48
  • Technically the title of your question is misleading. What you are doing is to *declare* an array (hence your question is being tagged as a duplicate). *Assigning values* to the individual items of that array is a different thing (and not covered in your examples of `workerSalaries`). – morido Feb 09 '16 at 22:48
  • I am sorry. Like I said, we learned this today, and don't fully grasp a lot of it – IronMaiden28 Feb 09 '16 at 22:49

0 Answers0