.

Arrays in JAVA

Saturday, 20 April 2013
After building basics in java ,now its time to pack your thoughts and come in the world of arrays.
you have already wanted to learn arrays but due to some way or another you didn't do that.your.probably your instructor or your friends call you dumb who can not learn arrays.so here is goes THE MAKING OF ARRAYS

An array is like a tray of cups.
Steps to declare an array.
step 1. Declare an int array variable .an array variable is a remote control to an array object.
            int[] nums;
step 2.  create a new int array with length of 7,and assign it to the previously declared int[] variable nums
             nums=new int[7];
step 3.  give each element in the array an int value.
              remember elements in an int array are just int variables.
    nums[0]=6;
    nums[1]=7;
    nums[2]=12;
    nums[3]=16;
    nums[4]=62;
    nums[5]=36;
    nums[6]=55;
you can also make arrays of Dogs  instead of nums..............

declare a DOG array variable
Dog[] pets;
create a new name array with a length of five ,and assign it to previously declared name[] variable names.
pets=new Dog[7];
create a new dog objects and assign them to the array elements.
pets[0]=new Dog();
pets[1]=new Dog();
............................
you can also access them as
Dog fido=new Dog();
fido.name="FIDO";
fido.bark(0;
fido.chasecat(0;
*********************************************************************************
programs
MATRIX ADDITION
import java.util.scanner.*;

class Addm
{
 public static void main(String[] arg)
 {
  int i,j;
 int a[][]=new int[3][3];
 Scanner sc=new Scanner(System.in);
 for(i=0;i<3;i++)
 {
   for(j=0;j<3;j++)
 {
  a[i][j]=sc.nextInt();
 }
}
  int b[][]=new int[3][3];
 for(i=0;i<3;i++)
 {
   for(j=0;j<3;j++)
 {
  b[i][j]=sc.nextInt();
}
}
int c[][]=new int[3][3];
 for(i=0;i<3;i++)
 {
   for(j=0;j<3;j++)
 {
  c[i][j]=a[i][j]+b[i][j];
 System.out.println("Matrix Addition="+c[i][j]);
}
}
}
}

*********************************************************************************
DIAGONAL OF MATRIX


import java.util.Scanner;
class Diagonalm
{
 public static void main(String[] arg)
 {
  int i,j;
 int a[][]=new int[3][3];
 Scanner sc=new Scanner(System.in);
 for(i=0;i<3;i++)
 {
   for(j=0;j<3;j++)
 {
  a[i][j]=sc.nextInt();
 }
}
System.out.println("Left diagonal elements=0");
 for(int k=0;k<3;k++)
 {
   for(int l=0;l<3;l++)
 {
 if(k==l)
 {
 System.out.println(a[k][l]);
}
}
}
 System.out.println("Right diagonal elements=0");
 for(int m=0;m<3;m++)
 {
   for(int n=0;n<3;n++)
 {
  if(m+n==2)
 {
  System.out.println(a[m][n]);
 }
}
}
}
}
*********************************************************************************
example to see a dog barking

class Dog
{
String name;
public static void main(string[] args)
{
//make a dog object and access it
Dog dog1=new Dog();
dog1.bark();
dog1.name="bart";
// now make a dog array
Dog[] mydogs=new Dog[3];
// put some dogs in it
mydogs[0]=new dog();
mydogs[1]=new dog();
mydogs[2]=dog1;
//now access dogs using arrays and references
mydog[0].name="fred";
mydogs[1].name="marge";
//now loop through the array
// nad tell all the dogs to bark
int x=0;
while(x<mydogs.length)
{
mydogs[x].bark();
x=x+1;
}
}

public void bark(0
{
System.out.println(name+"says ruff|");
}
public void eat()
{
}
public void chasecat()
{}
}


******************************************************************************
this was just enough to get your hands in array for nay further information comment below.



                   

.

Comment here