Results 1 to 5 of 5
  1. #1
    cjohnson412 is offline Member
    Join Date
    Nov 2008
    Posts
    3
    Rep Power
    0

    Lightbulb returning arrays

    Is it ok to return an array from a method, is it possible. I am currently trying to return this array with updates. When the number are returned it gives me the same numbers inputted. BTW, the method is suppose to multiply that amount in rtarray[i] and send it back to the main method.

    thanks

    Java Code:
    import javax.swing.JOptionPane;
    
    
    public class testPage {
    	public static void main(String[] args) {
    		
    		int[] numbers ={5,5,3,5,20};
    		int[] numbers2 = new int[5];
    		
    		
    		numbers2 = returnarray(numbers); 
    		
    		for(int i = 1; i<5; i++)
    		
    		JOptionPane.showMessageDialog(null,"this is array test" + numbers2[i]);
    	}
    	public static int[] returnarray(int[] rtarray)
    	{
    		int i=1;
    		final int otPayAM = 12;	
    		
    		
    		for(i=1; i>7; i++)
    			rtarray[i]*=otPayAM; 
    		
    		return rtarray;
    	}
    }
    Last edited by cjohnson412; 11-24-2008 at 04:28 PM. Reason: need to add what method is doing

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    What's your problem? Post an error message, if there is one, and clearly state your problem.
    I die a little on the inside...
    Every time I get shot.

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Logic problems...

    Your problem is that the logic in the for loop in the returnarray method is worng (very wrong)...

    Java Code:
    for(i=1; i>7; i++)
    This for loop is never executed because i is never greater than 7. According to me, this logic should be the same as the for loop you have in ain method.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    Yes it's possible and, yes, it's ok to do. Returning arrays have plenty of different uses.

  5. #5
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Actually, once you fix the other stuff, you will not need to return the array as you are doing it. An array in Java has full Object status, thus it amounts to what is called a reference in computer science.

    We have a consistent pattern in Java of being told to not modifiy something, and copy-semantics, all of which leads to endless discussions and blank stares from students.

    To get a mutable integer in Java, all that one need do is:
    Java Code:
    int [] integer = new int[1];
    integer = 1;
    doSomething(integer);
    and whamby-damby you can do something with an integer.

    This is discussed in all the Certification Study Guides so it is not overly hard to find.

    As you have it here, ops in returnarray can operate directly on the values in the array that was passed in and it will show up in the place it was called from.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. Why is my list returning nothing?
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 11-05-2008, 04:51 PM
  2. String returning null value
    By impact in forum New To Java
    Replies: 7
    Last Post: 08-03-2008, 08:49 AM
  3. returning String from actionPerformed
    By hardcore_teddy in forum New To Java
    Replies: 2
    Last Post: 05-17-2008, 06:32 AM
  4. returning a value from an arraylist
    By xkross in forum New To Java
    Replies: 2
    Last Post: 04-18-2008, 06:30 PM
  5. Replies: 0
    Last Post: 03-11-2008, 05:55 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
  •