Martrinex Learning Center
HOME CONTENTS
 
This website is underconstruction.
Author: Martin Sykes
Challenge: I have challenged myself to 1 article per day, and framework improvements! Enjoy
Challenge end: unknown

Arrays

We have covered a variables, an arrays is multiple variables with the same name, how could this be useful? Imagine you wanted to store the names of everyone who uses your program in memory...

Code:
String name0="martin";
String name1="joe";
String name2="moe";
String name3="";
String name4="";
This way is infeicient you cannot loop through the names their is no way to chance a variable in code, without an if statement eg, adding a name:
Code:
String tmpname="john";
if(name0.compareTo("")==0){

name0=tmpname;
} else if(name1.compareTo("")==0){

name1=tmpname;
} else if(name2.compareTo("")==0){

name2=tmpname;
} else if(name3.compareTo("")==0){

name3=tmpname;
} else if(name4.compareTo("")==0){

name4=tmpname;
} else{

System.out.println("memory full");
}
Their has to be a better way! and ofcourse their is, Arrays!
The sample below does the exact same as above but with Arrays.
Create a new project named 'namecheck' with a class named 'namecheck'
We will be optimising (improving) this in the next tutorial.
Code:
public class namecheck {

public static void main(String[] args) {

// Array declaration, like the sample above 5 different names 0,1,2,3,4 (NOT 5)
String name[] = new String[5];
// setting values
name[0]="martin";
name[1]="joe";
name[2]="moe";
name[3]="";
name[4]="";

String tmpname="john";
int i=0, set_index=-1;

// 0
if(name[i].compareTo("")==0){

set_index=i;
}
// 1
i++;
if(name[i].compareTo("")==0){

set_index=i;
}
// 2
i++;
if(name[i].compareTo("")==0){

set_index=i;
}
// 3
i++;
if(name[i].compareTo("")==0){

set_index=i;
}
// 4
i++;
if(name[i].compareTo("")==0){

set_index=i;
}

if(set_index==-1){

System.out.println("memory full");
} else{

name[set_index]=tmpname
}
}
}
Once again I have shown you a 'better' way and increased the size of the code, I haven't covered loops yet which I am covering in the next tutorial, for now note that with an Array the code repeats itself letter for letter 5 times. Since it allowed me to create an integer 'i' and increment it (add 1) 5 times doing the same check with the same code.

Without knowing loops I cannot go into any more details. Please read the next tutorial

Add Comment

Help the web master by commenting on the tutorials, thankyou.
Name:
Email: (this will not be shown)
Comment:
  Send email to admin, don't post.

 

Comments

Nobody has commented on this article.
Be the first, the webmaster needs comments good or bad to improve this site.



Copyright (c) 2008, Martin Sykes.
Learning Center is a branch of Martrinex Systems, Martrinex.net
Do not copy any materials from this site without permission from the auther.
Martrinex Learning Center[X]
  Introduction Beginner Intermediate

Important useful well commented source codes
Please read the import tutorial to know how to use them.

Source