Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [Tutorial] Arrays
Index -> Programming, Java -> Java Tutorials
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Tat




PostPosted: 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.



Arrays.doc
 Description:
better looks than the forum mode
but exact same stuff

Download
 Filename:  Arrays.doc
 Filesize:  22.5 KB
 Downloaded:  638 Time(s)

Sponsor
Sponsor
Sponsor
sponsor
Dan




PostPosted: Fri Feb 06, 2004 6:22 pm   Post subject: (No subject)

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

++bits; for u
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
Tat




PostPosted: Sat Feb 07, 2004 3:01 pm   Post subject: (No subject)

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




PostPosted: Tue Feb 17, 2004 5:49 pm   Post subject: (No 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.
Tony




PostPosted: Tue Feb 17, 2004 7:45 pm   Post subject: (No 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
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
wtd




PostPosted: Tue Feb 17, 2004 8:11 pm   Post subject: (No subject)

code:
strArray.length


If memory serves.
Maverick




PostPosted: Tue Feb 17, 2004 10:40 pm   Post subject: (No subject)

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




PostPosted: Tue Feb 17, 2004 10:59 pm   Post subject: (No subject)

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




PostPosted: Wed Oct 20, 2004 8:21 am   Post subject: (No 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.
wtd




PostPosted: Wed Oct 20, 2004 2:33 pm   Post subject: (No 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];
Leny




PostPosted: Wed Oct 20, 2004 4:04 pm   Post subject: (No subject)

thanks i'll try her out
Aziz




PostPosted: Fri Jun 24, 2005 8:57 pm   Post subject: (No subject)

What about flexible arrays? Sad That would cool.
wtd




PostPosted: Fri Jun 24, 2005 9:06 pm   Post subject: (No subject)

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


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




PostPosted: Fri Jun 24, 2005 11:41 pm   Post subject: (No 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/
flameZero




PostPosted: Thu May 11, 2006 8:04 am   Post subject: (No 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
Display posts from previous:   
   Index -> Programming, Java -> Java Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 22 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: