Adblock breaks this site

Introduction to Arrays

Discussion in 'Programming General' started by Linux, Jun 8, 2009.

  1. Linux

    Linux Forum Addict
    Banned

    Joined:
    Sep 2, 2008
    Posts:
    368
    Referrals:
    0
    Sythe Gold:
    0
    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
     
  2. Jimmy

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25
    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 :)
     
  3. Nullware

    Nullware Guru

    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0
    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
     
  4. Jimmy

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25
    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);
    }
     
  5. Linux

    Linux Forum Addict
    Banned

    Joined:
    Sep 2, 2008
    Posts:
    368
    Referrals:
    0
    Sythe Gold:
    0
    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
     
  6. Jimmy

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25
    Introduction to Arrays

    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.
     
  7. hampe-92

    hampe-92 Forum Addict

    Joined:
    Jul 10, 2008
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0
    Introduction to Arrays

    what do you know about that? I thought you 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
     
  8. Swan

    Swan When They Cry...
    Retired Global Moderator

    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner
    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.
     
  9. d great one

    d great one Apprentice

    Joined:
    Nov 4, 2007
    Posts:
    930
    Referrals:
    2
    Sythe Gold:
    0
    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?
     
  10. Jimmy

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25
    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.
     
  11. mu-b

    mu-b War is a Drug
    Banned

    Joined:
    Jun 24, 2009
    Posts:
    1,353
    Referrals:
    2
    Sythe Gold:
    0
    Introduction to Arrays

    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;} }
     
  12. Jimmy

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25
    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.
     
  13. mu-b

    mu-b War is a Drug
    Banned

    Joined:
    Jun 24, 2009
    Posts:
    1,353
    Referrals:
    2
    Sythe Gold:
    0
    Introduction to Arrays

     
  14. Jimmy

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25
    Introduction to Arrays

    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.
     
  15. mu-b

    mu-b War is a Drug
    Banned

    Joined:
    Jun 24, 2009
    Posts:
    1,353
    Referrals:
    2
    Sythe Gold:
    0
    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
     
  16. war833

    war833 Member

    Joined:
    Dec 11, 2008
    Posts:
    82
    Referrals:
    0
    Sythe Gold:
    0
    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.
     
  17. super_

    super_ Member

    Joined:
    Dec 20, 2008
    Posts:
    91
    Referrals:
    0
    Sythe Gold:
    0
    Introduction to Arrays

    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.
     
  18. war833

    war833 Member

    Joined:
    Dec 11, 2008
    Posts:
    82
    Referrals:
    0
    Sythe Gold:
    0
    Introduction to Arrays

    How come a for-each would be slightly faster, super_?
     
  19. SuF

    SuF Legend
    Pirate Retired Global Moderator

    Joined:
    Jan 21, 2007
    Posts:
    14,212
    Referrals:
    28
    Sythe Gold:
    1,234
    Discord Unique ID:
    203283096668340224
    <3 n4n0 Two Factor Authentication User Community Participant Spam Forum Participant Sythe's 10th Anniversary
    Introduction to Arrays

    I tested the times and the normal one was about 20ms faster when it looped through it 1000 times.... >_>.
     
  20. d great one

    d great one Apprentice

    Joined:
    Nov 4, 2007
    Posts:
    930
    Referrals:
    2
    Sythe Gold:
    0
    Introduction to Arrays

    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?
     
< Tunnel System With Server [Bypass Secruity at School, Work and Home] | Need private professional css hack coder >


 
 
Adblock breaks this site