Results 1 to 4 of 4
  1. #1
    kassoli is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

    Default Need Help with Arrays!

    Hi everyone

    im quite new to java... i was wondering if someone could help me understand what scambleA1Q1 in the code below does and explain to me the values of spot & longarrayParam[]

    also i was wondering if you could tell me how is it that when i run the program i get this result: (in which the elements of longArray are changed the second time around)

    Java Code:
    Before Mystery call:
    intVal: 3
    longArray: 0 1 4 9 16 
    
    After Mystery call:
    intVal: 3
    longArray: 9 1 4 0 16
    i would appreciate it if someone could help me out

    Java Code:
    /public class kassoli {
    
    		public static void main(String[] args) {
    		// Create variables and initialize them.
    		int intVal = 3;
    		long[] longArray = new long[5];
    		
    		for (int i = 0; i < longArray.length; i++) {
    			longArray[i] = i*i;
    		}
    		
    
    		System.out.println("Before Mystery call:");
    		System.out.println("intVal: " + intVal);
    		System.out.print("longArray: ");
    		for (int i = 0; i < longArray.length; i++) {
    			System.out.print(longArray[i] + " ");
    		}
    		System.out.print("\n\n");
    
    		// Call the mystery function...
    		scambleA1Q1(intVal, longArray);
    
    
    		System.out.println("After Mystery call:");
    		System.out.println("intVal: " + intVal);
    		System.out.print("longArray: ");
    		for (int i = 0; i < longArray.length; i++) {
    			System.out.print(longArray[i] + " ");
    		}
    		System.out.println();
    	}
    	
    
    	public static void scambleA1Q1(int spot, long[] longArrayParam) {
    		long longVar = longArrayParam[spot];
    		longArrayParam[spot] = longArrayParam[0];
    		longArrayParam[0] = longVar;
    		spot = 0;
    
    
    	}
    
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by kassoli View Post
    Java Code:
    public static void scambleA1Q1(int spot, long[] longArrayParam) {
    		long longVar = longArrayParam[spot];
    		longArrayParam[spot] = longArrayParam[0];
    		longArrayParam[0] = longVar;
    		spot = 0;
    
    
    	}
    I recognize a simple swap function when I see one: it swaps the element at index 'spot' with the element at index zero.

    kind regards,

    Jos

  3. #3
    ThrashingBoy is offline Member
    Join Date
    Apr 2010
    Location
    Phoenix, AZ
    Posts
    25
    Rep Power
    0

    Default

    Java Code:
    public static void scambleA1Q1(int spot, long[] longArrayParam) {
    		long longVar = longArrayParam[spot];
    		longArrayParam[spot] = longArrayParam[0];
    		longArrayParam[0] = longVar;
    		spot = 0;
    
    	}
    ScambleA1Q1 is a method (which is a segment of code that does something when the coders asks it too basically if you don't know), and the (int spot, long[] longArrayParam) is basically saying that in order to use this method you need to insert two things. First, int spot, which is just an integer with the name of spot, and long[] longArrayParam is about the same respectively.

    So when it's used...
    Java Code:
    // Call the mystery function...
    		scambleA1Q1(intVal, longArray);
    The integer that it requires you to put in as intVal which is equal to 3, and the longArray is the long[] that the method also asks for.

    So in that method, everywhere you see spot just understand that it is simple inputing 3 there, and when you see longArrayParam, just understand that it is inputing the longArray that was established outside of the method just after the public static void main(String[] args) line.

    You could easily rewrite this code and take out the method, but this is actually a pretty good way to teach methods I think.

  4. #4
    kassoli is offline Member
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

Similar Threads

  1. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 10:32 AM
  2. Arrays
    By PhQ in forum New To Java
    Replies: 3
    Last Post: 04-18-2010, 08:40 PM
  3. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  4. Help with arrays
    By Skorge in forum New To Java
    Replies: 1
    Last Post: 11-24-2009, 07:35 AM
  5. 2D Arrays
    By Major90 in forum New To Java
    Replies: 6
    Last Post: 11-06-2008, 02:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •