• Log in or sign up for an account here: https://www.sythe.org/login

Introduction to Arrays

Linux

Forum Addict
Banned
Joined
Sep 2, 2008
Sythe Gold
0
Messages
368
Introduction to Arrays
I just learned Arrays and I thought I would share some basics
You must have knowledge on Variables
Arrays are fun :)

Code:
class Arrays {
	public static void main(String[] args) {
		
		int[] Arrays; //I've declared an Array of integers which is defined by the []
		Arrays = new int[5]; //I've just created space for an Array that holds 5 integers
			
			Arrays[0] = 0;  //declaring each array
			Arrays[1] = 1;  //remember that it starts at 0 and not 1
			Arrays[2] = 2;
			Arrays[3] = 3;
			Arrays[4] = 4;
			
		System.out.println("This is number: " + Arrays[0]);
		System.out.println("This is number: " + Arrays[1]);
		System.out.println("This is number: " + Arrays[2]);
		System.out.println("This is number: " + Arrays[3]);
		System.out.println("This is number: " + Arrays[4]);
		
		}
		
	}

Output:

This is number: 0
This is number: 1
This is number: 2
This is number: 3
This is number: 4
 
Introduction to Arrays
Why not declare the array in an easier way and also use a for-loop to go through all of the values?

Code:
class Arrays {

    public static void main(String[] args) {
	int[] array = {
		0, 1, 2, 3, 4
	};
	for(int i : array) {
		System.out.println("This is number: " + Integer.toString(i));
	}
    }
}

Also, it's a single array, so "Arrays" wouldn't be a very good choice for a variable name.

Overall, decent tut. Things could have been done a bit easier, but it's an intro, so..yeah :)
 
Introduction to Arrays
Why not declare the array in an easier way and also use a for-loop to go through all of the values?

Code:
class Arrays {

    public static void main(String[] args) {
	int[] array = {
		0, 1, 2, 3, 4
	};
	for(int i : array) {
		System.out.println("This is number: " + Integer.toString(i));
	}
    }
}

Also, it's a single array, so "Arrays" wouldn't be a very good choice for a variable name.

Overall, decent tut. Things could have been done a bit easier, but it's an intro, so..yeah :)
Well since that's the case and it is an intro you should have used a regular for loop instead of the for each syntax.:p
 
Introduction to Arrays
Well since that's the case and it is an intro you should have used a regular for loop instead of the for each syntax.:p
Doesn't make a very big difference imo, but w/e

Code:
for(int i=0; i<array.length; i++) {
        int x = array[i];
        System.out.println("This is number: " + x);
}
 
Introduction to Arrays
lol this has nothing to do with loops
this is the standard way of doing it, and I like my way better, a lot cleaner and looks better
I like having good format
 
Introduction to Arrays
lol this has nothing to do with loops
this is the standard way of doing it, and I like my way better, a lot cleaner and looks better
I like having good format
The standard way for going through all the elements of an array is to go through each element manually? That almost defeats the whole purpose of an array.

Think about if you had 15 elements in the array. For my code, all you could have to do is add those elements into the array, and that's all. For your code, You would need to add 30 more lines of code that make the vast majority of the program bulky and unnecessary.
 
Introduction to Arrays
this is the standard way of doing it
what do you know about that? I thought you just learned arrays ^^
I just learned Arrays...

the "standard" way of doing it is with loops, preferably a for-each loop as Jimmy said...
A good rule of thumb (imo): three-four iterations or more, then use a loop
 
Introduction to Arrays
I find his nomenclature slightly amusing.

He's calling a single array "arrays" and he's calling each integer element an array.

But yes, loops are the standard way of initializing arrays and / or displaying their data. It's just common sense.
 
Introduction to Arrays
I have a question. How do you put mathematical operation in an array type?
Like example.

array[0] + array[1] = sum

Can you please give me syntax?

Oh, and one more question. How do you return the value of an array?
 
Introduction to Arrays
I have a question. How do you put mathematical operation in an array type?
Like example.

array[0] + array[1] = sum

Can you please give me syntax?

Oh, and one more question. How do you return the value of an array?

int sum = array[0] + array[1];

If by return the value, you mean get the value of all the elements in the array when added to each other, then

Code:
int sum=0;
for(int x : array) {
    sum += x;
}

at the end of the for-loop, sum will have the value of all the elements in the array added together.
 
Introduction to Arrays
int sum = array[0] + array[1];

If by return the value, you mean get the value of all the elements in the array when added to each other, then

Code:
int sum=0;
for(int x : array) {
    sum += x;
}

at the end of the for-loop, sum will have the value of all the elements in the array added together.

kuld just use a bubble sort ;
Code:
int[] sorted = new int[5]; int compare; for(int x = 0; x< 5; x++) { compare = number[x]; for(int y = 0; y<5; y++){if(compare > number[y]){compare=number[y];} sorted[x] = compare;} }
 
Introduction to Arrays
kuld just use a bubble sort ; int[] sorted = new int[5]; int compare; for(int x = 0; x< 5; x++) { compare = number[x]; for(int y = 0; y<5; y++){if(compare > number[y]){compare=number[y];} sorted[x] = compare;} }
I would help, but I "dnt lyk how u tyP," plus, the fact that you can't even be bothered to put your code in
Code:
 tags makes me assume that you don't care too about getting help.
 
Introduction to Arrays
I would help, but I "dnt lyk how u tyP," plus, the fact that you can't even be bothered to put your code in
Code:
 tags makes me assume that you don't care too about getting help.[/QUOTE]

lmfao im not the 1 askin 4 help. if you actually knew what you where talking about, instead just playing like you do then you could see that.
 
Introduction to Arrays
lmfao im not the 1 askin 4 help. if you actually knew what you where talking about, instead just playing like you do then you could see that.
My mistake- it's just that your English is so unreadable I mistook a statement for a question.

A suggestion: Type in proper English so that people are not completely confused while reading what you've typed.
 
Introduction to Arrays
u. dun. luk. clever. bi. puttin. a. fukin. ful. stop. after. evry. werd.

"Rofl, Excuse me? I talk like this because it makes me look more intelligent" - ye datz guna make me giv a shit
 
Introduction to Arrays
Quit being hard on him guys.

Some newbs would be even more confused if there was a loop in there. The example demonstrates an array just fine.
 
Introduction to Arrays
Doesn't make a very big difference imo, but w/e

Code:
for(int i=0; i<array.length; i++) {
        int x = array[i];
        System.out.println("This is number: " + x);
}

for-each would be slightly faster. the following code would produce the same instructions as the for-each:
Code:
int[] ax = array;
int len = ax.length;
for (int i = 0; i < len; ++i) {
    int x = ax[i];
    System.out.println("This is number: " + x);
}
Notice the length caching.
 
Introduction to Arrays
How come a for-each would be slightly faster, super_?
 
Introduction to Arrays
I tested the times and the normal one was about 20ms faster when it looped through it 1000 times.... >_>.
 
Introduction to Arrays
How come a for-each would be slightly faster, super_?

It is probably the declaration of variables? Which comes first. In where the first one will have to read .length over and over until the loop has ended?
 
Runescape Gamblingrunescape gambling
Back
Top