In this exercise, I'm supposed to sum up elements from 2 arrays, then display the answer on one line with spaces in between. I did it and my answer got accepted as correct, but I'm wondering if there is a better way to do this since I feel my way was more complicated than it needed to be.
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
int n = 11;
int[] firstArray = {811594, 993574, 299729,559604,161945,969851,588210,
692459, 28350,43017,797855};
int[] secondArray = {725888, 233750,191700,944750,380402,319860,766872,764921,
330218,906679, 65309};
List<Object> total = new ArrayList<>();
for(int i = 0; i < n; i++){
int sum = firstArray[i] + secondArray[i];
total.add(sum);
}
Integer[] sumArray = total.toArray(new Integer[total.size()]);
for(int i = 0; i < n; i++) {
System.out.print(sumArray[i] + " ");
}
}
}
5 Answers 5
You don't need to store the intermediate result. You don't need to give the length of the arrays. This assumes that the arrays are allways of equal length.
for(int i = 0; i < firstArray.length; i++){
System.out.print((firstArray[i] + secondArray[i]) + " ");
}
-
\$\begingroup\$ It's important to use .length over a hard coded "magic" number, as you then don't have to update it if your values change, and it becomes very flexible if you want dynamic content in your arrays. \$\endgroup\$Tom Bowen– Tom Bowen2016年01月14日 10:52:07 +00:00Commented Jan 14, 2016 at 10:52
@Orden is right that you don't need an intermediary array. On top of that, I would point out a few violations of good practices.
Use the right types. If you want to add integers in a list, then use a List<Integer>
instead of a List<Object>
.
If you know the final size of the list in advance, and you intend to convert it to an array, then consider skipping the list completely, and use just an array from the start. Like this:
int[] sumArray = new int[n];
for(int i = 0; i < n; i++){
sumArray[i] = firstArray[i] + secondArray[i];
}
-
\$\begingroup\$ IntelliJ IDEA made me change it to convert it to an array but it was List<Integer> before. \$\endgroup\$Yousef– Yousef2016年01月14日 00:20:07 +00:00Commented Jan 14, 2016 at 0:20
-
1\$\begingroup\$ @Yousef lesson 1 of using an advanced IDE, learn how it works. They don't make you do anything, they provide suggestions that speed up the process. You should not use the advanced functionality of your IDE at all until you have some knowledge of the language. \$\endgroup\$Boris the Spider– Boris the Spider2016年01月14日 14:56:52 +00:00Commented Jan 14, 2016 at 14:56
The basic idea is right, but I don't like this line and its consequences:
List<Object> total = new ArrayList<>();
- If you just need to print the result, you don't need to store the sums at all; you can just print as you go.
- If you want to store the sums, and you know the lengths of the inputs, then you can just make an array instead of an
ArrayList
to help you do it. - If you do use an
ArrayList
, then providing a capacity estimate would be good practice. List<Object>
defeats type safety.List<Integer>
would be better.
Also, instead of hard-coding n = 11;
, I suggest
assert firstArray.length == secondArray.length;
int n = firstArray.length;
So, one good solution would be
for (int i = 0; i < n; i++) {
System.out.print((firstArray[i] + secondArray[i]) + " ");
}
System.out.println(); // It's customary to finish with a newline
Another good solution would be
int[] sums = new int[n];
for (int i = 0; i < n; i++) {
sums[i] = firstArray[i] + secondArray[i];
}
for (int i = 0; i < n; i++) {
System.out.print(sums[i] + " ");
}
System.out.println();
Others have covered bad practices here, I am just going to demonstrate a Java 8 solution to the problem - as Java 8 is the current version of Java; I believe this is what should be written in modern Java:
assert firstArray.length == secondArray.length;
IntStream.range(0, firstArray.length)
.map(i -> firstArray[i] + secondArray[i])
.forEach(System.out::println);
This simply creates an IntStream
of [0, n)
and then uses those indices to map
to the sum of the variables.
-
\$\begingroup\$ That's look so weird from a old java coder point of view but at least I have learnt something new! :-) \$\endgroup\$Orden– Orden2016年01月14日 14:52:37 +00:00Commented Jan 14, 2016 at 14:52
-
\$\begingroup\$ @Orden certainly very un-Java like. But C# has had lambdas and LINQ for donkey's years and Scala changed the JVM language game somewhat. I find the syntax really readable and I find it enforces SRP as each distinct
map
operation has it's own, simple, function. \$\endgroup\$Boris the Spider– Boris the Spider2016年01月14日 14:54:58 +00:00Commented Jan 14, 2016 at 14:54 -
\$\begingroup\$ not the best way to use stream I would say. too bad java lacks a
zip
stream. \$\endgroup\$njzk2– njzk22016年01月14日 18:54:04 +00:00Commented Jan 14, 2016 at 18:54 -
\$\begingroup\$ @njzk2 Java 8 decided not to include tuple types so that meant that
zip
and friends could not be added. I have sorely missedzipWithIndex
numerous times in recent months... \$\endgroup\$Boris the Spider– Boris the Spider2016年01月15日 08:33:44 +00:00Commented Jan 15, 2016 at 8:33
The "modern" java solution puts newlines, not spaces between the elements, of course. Adding the trailing space, and using print instead of println will fix that. Inserting a join (Java has join, right?) might also work.
And, whats' wrong with APL's ⎕←firstArray+secondArray
-
\$\begingroup\$ It's really hard to understand what you're trying to say. "Inserting a join (Java has join, right?) might also work." sounds like guesswork, and doesn't make a good argument. \$\endgroup\$janos– janos2016年01月14日 16:16:11 +00:00Commented Jan 14, 2016 at 16:16
-
\$\begingroup\$ Join may not be the proper term. What I'm thinking of inserts a delimiter between a list of strings, creating one bigger string:/ 'one&two&three' == join('&',['one','two','three']) \$\endgroup\$Randy A MacDonald– Randy A MacDonald2016年01月21日 14:47:06 +00:00Commented Jan 21, 2016 at 14:47
-
\$\begingroup\$ To be honest, I thought my use of 'might' clearly implied some guesswork.... \$\endgroup\$Randy A MacDonald– Randy A MacDonald2016年01月21日 14:50:38 +00:00Commented Jan 21, 2016 at 14:50
-
\$\begingroup\$ Your answer as it is doesn't seem to make a clear, good point. I suggest to either rewrite it or delete it. \$\endgroup\$janos– janos2016年01月21日 15:03:40 +00:00Commented Jan 21, 2016 at 15:03
-
\$\begingroup\$ Having the elements separated by newlines seems incorrect. The Java solution also seems verbose, to this APL person at least. \$\endgroup\$Randy A MacDonald– Randy A MacDonald2016年02月12日 15:46:38 +00:00Commented Feb 12, 2016 at 15:46
List
, you can create an array immediately:int[] total = new int[n];
, thentotal[i] = sum;
\$\endgroup\$