This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Could someone explain how to solve to this problem? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Could someone explain how to solve to this problem?" Watch "Could someone explain how to solve to this problem?" New topic
Author

Could someone explain how to solve to this problem?

kenny gill
Ranch Hand

Joined: Mar 12, 2012
Posts: 54

I have no idea. I'd really appreciate some insight on how to solve it:

How would you refer to the last element in array A declared as

int[] A = { 1, 3, -4, 7 };
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5


or more general:


The quieter you are, the more you are able to hear.
kenny gill
Ranch Hand

Joined: Mar 12, 2012
Posts: 54


but what is it asking and how did you figure out that answer?
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

Java arrays start at index 0, so in your example A[0] == 1

A.length returns the true length, in this case 4, so you must subtract 1 to get the last entry.

TIP: Be careful if length == 0, since you will then try to index A[-1] which will fail.

WP
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14193
    
  20

To learn what exactly arrays are and how they work, see Arrays in Oracle's Java Tutorials.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Could someone explain how to solve to this problem?