Tech Blog   »   Home   »   Coders Community   »   Computer Directory   »   C++ Operators   »   IP Subnetting   »  

Source Code Database

Programming Examples | Source Code Examples
It is currently Sat Nov 01, 2014 3:28 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: JAVA: Pass an Array to a Method
PostPosted: Fri Aug 13, 2010 1:45 pm 
Offline
Site Admin
User avatar

Joined: Mon Aug 02, 2010 4:20 pm
Posts: 74
Location: SoCal
An example of an array and also passing it to a method using .length:

Code:
import java.util.Scanner;
public class PassArrayToMethod
{
   public static void main(String[] args)
   {
      int size = 5;
      double scores [] = new double [size]; // Declare and instantiate an array.
      getScores(scores);    // Method call
      showScores(scores);   // Method call
   }
   
   // Method - Cannot see what's in main, use the .length to pass the size of array.
   public static void getScores(double scores[])
   {
      Scanner scan = new Scanner(System.in);
      System.out.println("Enter " + scores.length  + " test scores: ");
      
      for (int i = 0; i < scores.length ; i++)
      {
         scores[i] = scan.nextDouble();
      }      
   }
   
   // Method - Cannot see what's in main, use the .length to pass the size of array.
   public static void showScores(double scores [])
   {
      System.out.println("\nHere are the scores: ");
      
      for ( int i = 0; i < scores.length; i++)
      {
         System.out.println(scores[i]);
      }
   }
}

*/==============[output]==================
Enter 5 test scores:
88.3
81.7
92.6
89
92

Here are the scores:
88.3
81.7
92.6
89.0
92.0
========================================*/


Top
 Profile  
 
 Post subject: Re: JAVA: Pass an Array to a Method
PostPosted: Mon Aug 16, 2010 5:55 am 
Offline

Joined: Tue Aug 03, 2010 11:33 am
Posts: 18
I am learning c++, the arrays here in Java look the same as in c++, accept for the the scan thing. I have not learned java yet but looking at this programs source code it looks like some of the syntax is related. The variables are declared the same as well?


Top
 Profile  
 
 Post subject: Re: JAVA: Pass an Array to a Method
PostPosted: Thu Aug 19, 2010 2:15 pm 
Offline
Site Admin
User avatar

Joined: Mon Aug 02, 2010 4:20 pm
Posts: 74
Location: SoCal
Yes, JAVA is derived from C++... :ugeek:


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

cron


HTML Feed


Powered by phpBB © 2000, 2002, 2005, 2007, 2010-2012 phpBB Group
About | Copyright © 2010-2012 Contact CodeObsessed.com | Privacy Policy

Home Page

Validated with no errors:

Valid XHTML 1.0 Transitional     Valid CSS!