Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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 help

There 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:
/** First array */

public class enford
  {
  public static void main(String[] args)
     {  
     int count = 0;
     float[] costs;
     costs = new float[5];

     while (count < 5)
        {
        System.out.print("Data: ");
        costs[count] = wellreader.read_number();
        if (costs[count] <= 0.0) break;
        count++;
        }

     for (int k=count-1; k>=0; k--)
        System.out.println(costs[k]);

     }  
  }


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.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho