| |||||||||||
Need help on Arrays Urgently ??? Posted by Java_Learner (Java_Learner), 23 March 2003 hi.. there. Is there any great java programmers out there who can help me on Arrays ? I am really lose in it and i am trying to create a programme using Arrays. So if anyone out there who is kind enough to help me pls reply in either this forum or emailing me thks.. my email add is hikaru_08@hotmail.com Posted by admin (Graham Ellis), 23 March 2003 I've been updating the section on Array in our Java course over the last couple of days .... here's how it starts - it may helpThere will be frequent occasions when you'll want to perform the same operation on a whole series of primitives or objects one after the other, and it won't be practical to hold each in a separate named variable. Instead of using individual variables, we'll use a whole number of variables all in one: * We'll give the whole thing a single name * We'll access elements by their numeric position in the table * And we'll call the whole thing an array Let's see an example. We'll: * Create an array * Read a number of float values into it * Write out the values back-to-front (Clearly this is one of those cases where we have to store what could be a lot of numbers.) Code:
Let's look through the various parts of that. * Definition and declaration Just like other variables we have seen, you must define that the variable called "costs" is going to contain float information, and you must also declare it's going to be an array: float[] costs; That has defined how the variable name will be used, but has not set aside any memory for it. Since computers store things one-after-another in memory, we must actually create our array, declaring its size: costs = new float[5]; In Java, the array is actually an object created by using the new method. It is important that you understand the distinction between defining an array and actually allocating the memory for it, although in practice you could do both in one line: float [] costs = new float [5]; * Use We have defined that a variable name is to be used as an array, and we have set aside the memory for it. How do we actually make use of it? We refer to the array name, and then the element number in square brackets. Elements are counted from 0 up, so our five-element array is numbered zero to four. As well as referring to elements by giving an integer constant, we can use an integer variable, or even an integer expression within the square brackets, so that we calculate the element number. And we can use array elements anywhere where we could use just a simple variable of the same type. Thus, in our sample program, we had: costs[count] = wellreader.read_number(); which set an element of the array to contain the number read from the user, and: if (costs[count] <= 0.0) break; which used an element of the array in a calculation (in this case to yield a boolean result), and: System.out.println(costs[k]); This page is a thread posted to the opentalk forum
at www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |