Online Course Discussion Forum
AP computer science A
what is wrong with the output? what should I write for the System.out.println ?
Hi Cassie,
Backing up a second, it's always good to think about exactly what your method is trying to accomplish. Do you want to swap the first and last elements in the array and then print out all the elements? Or do you want to return the array after the elements are swapped?
If you're trying to print out the elements, you probably will need to write a loop. Right now you're asking the program to "print" the array, which is an object, not a string, and that is causing the unintended behavior. Alternatively, there are some Java libraries that might help. Try searching something like "Arrays.toString()" online to practice using online resources.
Let us know if you have any more questions!
so how can I add a string to the new array after swap it? I'm trying to output the array after swap the first and last element.
alright I think I get it now
This last one seems to work fine.
There are a few things to pay attention though. The class name should start with a capital letter. This is not required by syntax, but it's the conventional style.
Also make the indentations consistent. Always indent by the same number of spaces (our textbook authors use 2 spaces; in the corporate world, 3 or 4 spaces is the most common. Be consistent.
Keeping good style is a habit that you develop while you are learning. Once it becomes a habit, you won't even realize that you are following the good guideline. The modern IDEs such as Eclipse automatically does it for you, not just while you are typing, but also have a menu item to "beautify" your code for you. When you are writing programs in big projects, your code can become cluttered over time and may be chaotic. Using good style is always helpful, so it's better to develop this habit.
社交网络