"यहां दिलचस्प चीज़ों के कुछ उदाहरण दिए गए हैं जिन्हें आप सरणियों के साथ कर सकते हैं:"

उदाहरण 1।
1 से 10 तक की संख्याओं के साथ 10-तत्वों की सरणी भरें:
public class MainClass
{
    public static void main(String[] args) 
    {
        int[] numbers = new int[10];

        for (int i = 0; i < numbers.length; i++)
        {
           numbers[i] = i + 1;
        }
    }
}
10 से 1 तक की संख्या के साथ 10-तत्व सरणी भरें:
public class MainClass
{
    public static void main(String[] args) 
    {
        int[] numbers = new int[10];

        for (int i = 0; i < numbers.length; i++)
        {
           numbers[i] = 10 - i;
        }
    }
}
0 से 9 तक की संख्या के साथ 10-तत्व सरणी भरें:
public class MainClass
{
    public static void main(String[] args) 
    {
        int[] numbers = new int[10];

        for (int i = 0; i < numbers.length; i++)
        {
           numbers[i] = i;
        }
    }
}
9 से 0 की संख्या के साथ 10-तत्व सरणी भरें:
public class MainClass
{
    public static void main(String[] args) 
    {
        int[] numbers = new int[10];

        for (int i = 0; i < numbers.length; i++)
        {
           numbers[i] = 9 - i;
        }
    }
}
उदाहरण 2।
कीबोर्ड से 10 तार पढ़ें:
public class MainClass
{
  public static void main(String[] args) throws IOException
  {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    String[] list = new String[10];

    for (int i = 0; i < list.length; i++)
    {
      list[i] = reader.readLine();
     }
  }
}
कीबोर्ड से 10 नंबर पढ़ें:
public class MainClass
{
  public static void main(String[] args) throws IOException
  {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    int[] list = new int[10];

    for (int i = 0; i < list.length; i++)
    {
      String s = reader.readLine();
      list[i] = Integer.parseInt(s);
    }
  }
}
उदाहरण 3।
स्क्रीन पर एक सरणी प्रदर्शित करें:
public class MainClass
{
    public static void main(String[] args) throws IOException
    {
        int[] list = new int[10];

        // Fill the array
        for (int i = 0; i < list.length; i++) 
           list[i] = i;

        // Display the contents of the array
        for (int i = 0; i < list.length; i++)  
          System.out.println(list[i]);
    }
}
उदाहरण 4।
त्वरित (स्थैतिक) आरंभीकरण। सरणी तत्वों को जोड़ें:
public class MainClass
{
    public static void main(String[] args) throws IOException
    {
        // Static initialization
        int[] list = {5, 6, 7, 8, 1, 2, 5, -7, -9, 2, 0};        
        
        // Calculate the sum
        int sum = 0;
        for (int i = 0; i < list.length; i++) 
           sum += list[i];

        System.out.println("Sum is " + sum);
    }
}
उदाहरण 5।
सरणी में सबसे छोटा तत्व खोजें:
public class MainClass
{
    public static void main(String[] args) throws IOException
    {
        int[] list = {5, 6, 7, 8, 1, 2, 5, -7, -9, 2, 0};

        int min = list[0];
        for (int i = 1; i < list.length; i++)
        {
             if (list[i] < min) 
                  min = list[i];
        }

       System.out.println ("Min is " + min);
    }
}

Codegym यूनिवर्सिटी कोर्स के भाग के रूप में एक परामर्शदाता के साथ एक व्याख्यान स्निपेट। पूर्ण पाठ्यक्रम के लिए साइन अप करें।