Wednesday, September 28, 2016

Java- Arrays

This program shows demo on how to implement arrays in Java. Have Fun!!

class Customer
{
    int id;
    String name;
    public Customer()
    {
        id=0;
        name="";
    }
    public Customer(int i,String n)
    {
        id=i;
        name=n;
    }
}
class ArraysDemo
{
    public static void main(String[] args)
    {
        Customer[] c= new Customer[2]; //object array
        c[0]=new Customer(); //need to initialize
        c[1]=new Customer();
        System.out.println(c[0].name);
        int[] a=new int[10];
        String[] str = new String[5];
        for(String s:str)
        {
            System.out.println(s);
        } //for each loop on string type
        for(int v:a)
        {
            System.out.println(v);
       
        } //for each loop
            for(int i=0;i<a.length;i++)
        {   
            a[i]= new java.util.Scanner(System.in).nextInt();
        }
        for(int i=0;i<a.length;i++)
        {
            System.out.println(a[i]);
        }
        /*System.out.println(i);*/ //error due to scope
        System.out.println(a);
    }
}

No comments:

Post a Comment