Introduction to Arrays

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

Introduction to Arrays
  1. Unread #1 - Jun 8, 2009 at 1:52 AM
  2. Linux
    Joined:
    Sep 2, 2008
    Posts:
    368
    Referrals:
    0
    Sythe Gold:
    0

    Linux Forum Addict
    Banned

    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
     
  3. Unread #2 - Jun 12, 2009 at 10:40 PM
  4. Jimmy
    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    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 :)
     
  5. Unread #3 - Jun 13, 2009 at 3:49 PM
  6. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    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
     
  7. Unread #4 - Jun 13, 2009 at 6:12 PM
  8. Jimmy
    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    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);
    }
     
  9. Unread #5 - Jun 14, 2009 at 1:33 AM
  10. Linux
    Joined:
    Sep 2, 2008
    Posts:
    368
    Referrals:
    0
    Sythe Gold:
    0

    Linux Forum Addict
    Banned

    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
     
  11. Unread #6 - Jun 14, 2009 at 10:17 AM
  12. Jimmy
    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    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.
     
  13. Unread #7 - Jun 14, 2009 at 2:53 PM
  14. hampe-92
    Joined:
    Jul 10, 2008
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0

    hampe-92 Forum Addict

    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
     
  15. Unread #8 - Jun 14, 2009 at 5:15 PM
  16. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    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.
     
  17. Unread #9 - Jun 24, 2009 at 11:21 AM
  18. d great one
    Joined:
    Nov 4, 2007
    Posts:
    930
    Referrals:
    2
    Sythe Gold:
    0

    d great one Apprentice

    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?
     
  19. Unread #10 - Jun 24, 2009 at 1:08 PM
  20. Jimmy
    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    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.
     
  21. Unread #11 - Jun 24, 2009 at 9:34 PM
  22. mu-b
    Joined:
    Jun 24, 2009
    Posts:
    1,353
    Referrals:
    2
    Sythe Gold:
    0

    mu-b War is a Drug
    Banned

    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;} }
     
  23. Unread #12 - Jun 26, 2009 at 2:33 PM
  24. Jimmy
    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    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.
     
  25. Unread #13 - Jun 26, 2009 at 2:43 PM
  26. mu-b
    Joined:
    Jun 24, 2009
    Posts:
    1,353
    Referrals:
    2
    Sythe Gold:
    0

    mu-b War is a Drug
    Banned

    Introduction to Arrays

     
  27. Unread #14 - Jun 26, 2009 at 4:35 PM
  28. Jimmy
    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    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.
     
  29. Unread #15 - Jun 26, 2009 at 4:47 PM
  30. mu-b
    Joined:
    Jun 24, 2009
    Posts:
    1,353
    Referrals:
    2
    Sythe Gold:
    0

    mu-b War is a Drug
    Banned

    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
     
  31. Unread #16 - Sep 16, 2009 at 11:25 AM
  32. war833
    Joined:
    Dec 11, 2008
    Posts:
    82
    Referrals:
    0
    Sythe Gold:
    0

    war833 Member

    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.
     
  33. Unread #17 - Sep 16, 2009 at 4:59 PM
  34. super_
    Joined:
    Dec 20, 2008
    Posts:
    91
    Referrals:
    0
    Sythe Gold:
    0

    super_ Member

    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.
     
  35. Unread #18 - Sep 16, 2009 at 5:30 PM
  36. war833
    Joined:
    Dec 11, 2008
    Posts:
    82
    Referrals:
    0
    Sythe Gold:
    0

    war833 Member

    Introduction to Arrays

    How come a for-each would be slightly faster, super_?
     
  37. Unread #19 - Sep 16, 2009 at 10:37 PM
  38. SuF
    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

    SuF Legend
    Pirate Retired Global Moderator

    Introduction to Arrays

    I tested the times and the normal one was about 20ms faster when it looped through it 1000 times.... >_>.
     
  39. Unread #20 - Sep 17, 2009 at 5:32 AM
  40. d great one
    Joined:
    Nov 4, 2007
    Posts:
    930
    Referrals:
    2
    Sythe Gold:
    0

    d great one Apprentice

    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 >

Users viewing this thread
1 guest


 
 
Adblock breaks this site