adsense


Showing posts with label JAVA EXAMPLES PRINTING ARRAY USING METHOD. Show all posts
Showing posts with label JAVA EXAMPLES PRINTING ARRAY USING METHOD. Show all posts

Monday, 26 October 2015

JAVA EXAMPLES PRINTING ARRAY USING METHOD PAGE 79

COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button



JAVA EXAMPLES PRINTING ARRAY USING METHOD



Java Examples - Printing Array using Method


Problem Description:
How to display Object class using instanceOf keyword?
Solution:
This example makes displayObjectClass() method to display the Class of the Object that is passed in this method as an argument.
import java.util.ArrayList;
import java.util.Vector;

public class Main {

public static void main(String[] args) {
   Object testObject = new ArrayList();
      displayObjectClass(testObject);
   }
   public static void displayObjectClass(Object o) {
      if (o instanceof Vector)
      System.out.println("Object was an instance 
      of the class java.util.Vector");
      else if (o instanceof ArrayList)
      System.out.println("Object was an instance of 
      the class java.util.ArrayList");
      else
      System.out.println("Object was an instance of the " 
      + o.getClass());
   }
}
Result:
The above code sample will produce the following result.
Object was an instance of the class java.util.ArrayList


Sunday, 25 October 2015

JAVA EXAMPLES PRINTING ARRAY USING METHOD PAGE 74

COMPUTER LANGUAGES HTML,C,C++.JAVA,.NET AND MULTIMEDIA basics and programs click home button



JAVA EXAMPLES PRINTING ARRAY USING METHOD



Java Examples - Printing Array using Method

Problem Description:
How to use method overloading for printing different types of array ?
Solution:
This example displays the way of using overloaded method for printing types of array (integer, double and character).
public class MainClass {
   public static void printArray(Integer[] inputArray) {
      for (Integer element : inputArray){
         System.out.printf("%s ", element);
         System.out.println();
      }
   }
   public static void printArray(Double[] inputArray) {
      for (Double element : inputArray){
         System.out.printf("%s ", element);
         System.out.println();
      }
   }
   public static void printArray(Character[] inputArray) {
      for (Character element : inputArray){
         System.out.printf("%s ", element);
         System.out.println();
      }
   }
   public static void main(String args[]) {
      Integer[] integerArray = { 1, 2, 3, 4, 5, 6 };
      Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4,
      5.5, 6.6, 7.7 };
      Character[] characterArray = { 'H', 'E', 'L', 'L', 'O' };
      System.out.println("Array integerArray contains:");
      printArray(integerArray);
      System.out.println("\nArray doubleArray contains:");
      printArray(doubleArray);
      System.out.println("\nArray characterArray contains:");
      printArray(characterArray);
   }
}
Result:
The above code sample will produce the following result.
Array integerArray contains:
1 
2 
3 
4 
5 
6 

Array doubleArray contains:
1.1 
2.2 
3.3 
4.4 
5.5 
6.6 
7.7 

Array characterArray contains:
H 
E 
L 
L 
O