इस पाठ में ऐरे का उदाहरण है आप इससे जान सकते है कि जावा प्रोग्रामिंग में किस प्रकार एक ऐरे का उपयोग होता हैं। कंप्यूटर प्रोग्रामिंग में ऐरे का उपयोग रैम में मेमोरी ब्लॉक बनाने में किया जाता हैं। ऐरे का उदाहरण चरो की शृंखला भी हो सकता है जिसमें 1-n तक चर होते है।

arrays.java सम्पादन

// यह प्रोग्राम तापमान रूपांतरण तालिकाओं को प्रदर्शित करने के लिए एरेज़ का उपयोग करता है

import java.util.*;

class arrays
{
    private static Scanner input = new Scanner(System.in);
    
    public static void main(String[] args) 
    {
        double[] c; //c चर की घोषणा
        double[] f; //f की घोषणा
        
        c = buildC(100);
        f = buildF(212);
        displayArray("C", c);
        displayArray("F", f);
        findTemperature(c, f);
    }

    private static double[] buildC(int size) 
    {
        double[] c = new double[size + 1];
        int index;
        
        for (index = 0; index <= size; index += 1) 
        {
            c[index] = (double) index * 9 / 5 + 32;
        }
        
        return c;
    }

    private static double[] buildF(int size) 
    {
        double[] f = new double[size + 1];
        int index;
        
        for (index = 0; index <= size; index += 1) 
        {
            f[index] = (double) (index - 32) * 5 / 9;
        }
        
        return f;
    }

    private static void displayArray(String name, double[] array) 
    {
        int index;
        
        for (index = 0 ; index <= array.length - 1 ; index += 1) 
        {
            System.out.println(name + "[" + index + "] = " + array[index]);
        }
    }

    private static void findTemperature(double[] c, double[] f) 
    {
        int temp;
        int size;
        
        size = minimum(c.length, f.length);
        do 
        {
            System.out.println("Enter a temperature between 0 and " + 
                Integer.toString(size - 1));
            temp = input.nextInt();
        } while (temp < 0 || temp > size - 1);
        System.out.println(Integer.toString(temp) + 
            "° Celsius is " + c[temp] + "° Fahrenheit");
        System.out.println(Integer.toString(temp) + 
            "° Fahrenheit is " + f[temp] + "° Celsius");
    }

    private static int minimum(int value1, int value2) 
    {
        int result;
        
        if (value1 < value2)
        {
            result = value1;
        }        
        else
        {
            result = value2;
        }

        return result;
    }
}

कोशिश करो सम्पादन

निम्न कोड मुफ्त ऑनलाइन विकास के वातावरण में से एक में ऊपर कॉपी और पेस्ट करो या अपने खुद के जावा कम्पाइलर/इंटरप्रेटर/आईडीई का उपयोग करें।

यह भी देखें सम्पादन