Monday, June 29, 2015

my{} JAVA array app source

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

/**
 *
 * @author Class
 */
import javax.swing.JOptionPane;
public class JavaApplication1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
    
        int max = -999; 
 
    int[] myArray = {5, 8, 2, 10, 9, 4}; 
    
    
    JOptionPane.showMessageDialog(null, "Here are the list of numbers"  );
    for (int i = 0; i < myArray.length; i++) {
  

    JOptionPane.showMessageDialog(null, myArray[i], "Member of the Array" , JOptionPane.CLOSED_OPTION);}
    JOptionPane.showMessageDialog(null,"In the array, the larget value is " 
                        + getLargest(myArray, max) + "."); 

 }
public static int getLargest(int[] myArray, int max) 
{     
    int i = 0, j = 0, tempmax = 0; 
    if (myArray.length == 1) 
    { 
        return myArray[0] > max ? myArray[0] : max; 
    } 
    else if (max < myArray[i]) 
    { 
        max = myArray[i]; 
        int[] tempArray = new int[myArray.length-1]; 
        for (i = 1; i < myArray.length; i++) 
        { 
            tempArray[j] = myArray[i]; 
            j++; 
        } 
        tempmax = getLargest(tempArray, max); 
        return tempmax; 
    } 
    else
    { 
        int[] tempArray = new int[myArray.length-1]; 
        for (i = 1; i < myArray.length; i++) 
        { 
            tempArray[j] = myArray[i]; 
            j++; 
        } 
        tempmax = getLargest(tempArray, max); 
        return tempmax; 
    } 


}

No comments:

Post a Comment