Computer Science Canada

[Tutorial] Arrays

Author:  Tat [ Fri Feb 06, 2004 4:51 pm ]
Post subject:  [Tutorial] Arrays

Arrays
Most programming languages, including Java, provide a data structure called an array, which consists of an ordered collection of similar items. An array, as a whole, has a single name, and the items in an array are referred to in terms of their position within the array.
If we needed to declare 20 tests you probably would do it like this:
int test1, test2, test3, test4, test5, test6, test7, test8, test9, test10, test11, test12, test13, test14, test15, test16, test17, test18, test19, test20;
All this work is not necessary, we could do it all in 1 small non confusing way with arrays like this:
int[] tests = new tests[20];
To call on the first test you would call on tests[0], the second test you would call on test[1] and so on and up to the last test tests[19].

How would you call on all these tests, don't tell me you have to type tests[0], tests[1]"¦?
You actually can do everything in a for loop, say I want to calculate the average of all these tests I would use:
int sum=0;
int avg;
for(int i=0; i<tests.length(); i++)
sum+=test[i];
avg=sum/tests.length();
Sounds familiar? This is basically review except it's a for loop with ARRAYS.

There can be arrays for strings too using the split() method to split a string into separate pieces.
Ex: String str= "I am GOD";
String []strArray=str.split(" ");
What's this? It simply means it's splitting the spaces for str which now has 3 parts.
So does that mean I can call on "I" as the first part but what kind of code would I use to express "I"?
strArray[0] = "I";
strArray[1] = "am";
strArray[2] = "GOD";
To display the original str I would concatenate like this:
System.out.println(strArray[0] + " " + strArray[1] + " " + strArray[2]);
5 short answer questions
1) How would you declare 20 names with arrays?
String[] names = new names[20];
2) How would you use a for loop to display the names from last to first on the list?
for(int i=0; i<names.length(); i++)
System.out.println(names[i] + " ");
3) What is an array?
An array consists of an ordered collection of similar items.
4) What is split() used for?
It is a method to split a string into separate pieces.
5) String str= "You are stupid";
String []strArray=str.split(" ");
How would you call on "stupid"?
strArray[2]

Sample Problem(small coding assignment)
Write a program that asks users for the tests he had taken this semester with all the subjects and stuff. Then find the average for each subject and also find the term average.

Author:  Dan [ Fri Feb 06, 2004 6:22 pm ]
Post subject: 

not a bad tutorial, could uses some [code], [list] and [b] tags to make it look nicer tho....

++bits; for u

Author:  Tat [ Sat Feb 07, 2004 3:01 pm ]
Post subject: 

it looks alot better if u open the attachment instead since i wrote it with microsoft words

Author:  nate [ Tue Feb 17, 2004 5:49 pm ]
Post subject: 

when you do the code

String str= "I am GOD";
String []strArray=str.split(" ");

Lets say instead you read it from a file without knowing what the sentence would be. How would you know how many strings you have in your array.

Author:  Tony [ Tue Feb 17, 2004 7:45 pm ]
Post subject: 

the array probly has a method such as .size or .upperBound or something Confused that returns the number of elements in it... sorry, can't tell the exact syntax, I dont have an IDE to check with at the moment

Author:  wtd [ Tue Feb 17, 2004 8:11 pm ]
Post subject: 

code:
strArray.length


If memory serves.

Author:  Maverick [ Tue Feb 17, 2004 10:40 pm ]
Post subject: 

these like arrays in turing pretty much, lie the same function?

Author:  wtd [ Tue Feb 17, 2004 10:59 pm ]
Post subject: 

http://java.sun.com/developer/onlineTraining/JavaIntro/exercises/Arrays/

Author:  Leny [ Wed Oct 20, 2004 8:21 am ]
Post subject: 

Hey does anyone know how to store graphics into an array? like if it's number 1 draw this, type thingger. so i don't have to keep doing them.

Author:  wtd [ Wed Oct 20, 2004 2:33 pm ]
Post subject: 

Leny wrote:
Hey does anyone know how to store graphics into an array? like if it's number 1 draw this, type thingger. so i don't have to keep doing them.


Ummm...

code:
Graphic[] graphicsArray = new Graphic[someNumberOfGraphics];

Author:  Leny [ Wed Oct 20, 2004 4:04 pm ]
Post subject: 

thanks i'll try her out

Author:  Aziz [ Fri Jun 24, 2005 8:57 pm ]
Post subject: 

What about flexible arrays? Sad That would cool.

Author:  wtd [ Fri Jun 24, 2005 9:06 pm ]
Post subject: 

Aziz wrote:
What about flexible arrays? Sad That would cool.


Check out Java's various collection classes, like ArrayList.

Author:  Hikaru79 [ Fri Jun 24, 2005 11:41 pm ]
Post subject: 

Aziz wrote:
What about flexible arrays? Sad That would cool.

I wrote an introductory tutorial on that, here: http://www.compsci.ca/v2/viewtopic.php?t=7070

In the meantime, though, I've discovered better ways to do this sort of thing, and the array model is not the only type of data structure out there. Have a look: http://java.sun.com/docs/books/tutorial/collections/

Author:  flameZero [ Thu May 11, 2006 8:04 am ]
Post subject: 

how would you let the user input the variables into an array? say if you have a 10 string array and you wanted the user to input all 10 strings. Embarassed

Author:  cool dude [ Thu May 11, 2006 3:28 pm ]
Post subject: 

this is a sample program i made to show you how. i might be mistaken because i just started java but this is using my knowledge from other languages

code:

import java.io.*;

public class arrays {
    public static void main (String[]args){
        int[] numbers = new int[5];
        String number;
        try{
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            for(int i = 0; i<5; i++){
            System.out.println("enter 5 numbers");
            number = in.readLine();
            numbers[i] = Integer.parseInt(number);   
            }
            for(int j = 0; j<5; j++){
                System.out.println("The numbers you entered are " + numbers[j]);
            }
        }catch(IOException nfe){
        }
    }
}

Author:  Krabjuice [ Fri May 12, 2006 2:53 pm ]
Post subject: 

Well, if you already know how large the array is, a simple for loop is all you need.

Make sure you start your loop at 0, as the index for an array begins at 0--not 1. Thus start=0, end=(size-1)

For instance:

code:

import java.util.Scanner;
public class ArrayInput
{
     public static void main (String[] args)
     {
          Scanner input = new Scanner(System.in);
          int[] get = new int[10];
          for (int i=0;i<=9;i++)
          {
               get[i]=input.nextInt();
          }
     }
}

Author:  cool dude [ Fri May 12, 2006 3:44 pm ]
Post subject: 

Krabjuice wrote:
Well, if you already know how large the array is, a simple for loop is all you need.

Make sure you start your loop at 0, as the index for an array begins at 0--not 1. Thus start=0, end=(size-1)

For instance:

code:

import java.util.Scanner;
public class ArrayInput
{
     public static void main (String[] args)
     {
          Scanner input = new Scanner(System.in);
          int[] get = new int[10];
          for (int i=0;i<=9;i++)
          {
               get[i]=input.nextInt();
          }
     }
}


dude thats pretty much the same thing i wrote! except your using scanner to get input and i'm using bufferedreader.

Author:  Krabjuice [ Fri May 12, 2006 7:50 pm ]
Post subject: 

True. However, not everyone uses the classic buffered reader method. Pick and choose, it would seem the situation is.

Author:  cool dude [ Fri May 12, 2006 7:55 pm ]
Post subject: 

Krabjuice wrote:
True. However, not everyone uses the classic buffered reader method. Pick and choose, it would seem the situation is.


actually i would have chosen the scanner method but unfortunately it seems not to work, even though i downloaded the latest version of java from the javasun website.

Author:  Krabjuice [ Fri May 12, 2006 8:02 pm ]
Post subject: 

Well, now we have two excellent examples of input and array manipulation.

Author:  JMG [ Mon Feb 25, 2008 10:14 am ]
Post subject:  RE:[Tutorial] Arrays

what are the limit to arrays in java? good tut btw XD


: