Posted: 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
Posted: 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 CanadaHelp with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
Tat
Posted: 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
Posted: 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
Posted: Tue Feb 17, 2004 7:45 pm Post subject: (No subject)
the array probly has a method such as .size or .upperBound or something 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
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
Posted: 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.