SlideShare une entreprise Scribd logo
1  sur  25
ARRAYS
ConCepts
Ten variables
Process 10 variables
An array of scores
Arrays
Arrays are generally effective means of storing groups of variables.
An array is a group of variables that share the same name and are
 ordered sequentially from zero to one less than the number of
 variables in the array.
The number of variables that can be stored in an array is called the
 array's dimension.
Each variable in the array is called an element of the array.
The scores array
Creating Arrays
There are three steps to creating an array,
  declaring it,
  allocating it and
  initializing it.
Declaring Arrays
 Like other variables in Java, an array must have a specific type like
  byte, int, String or double.
 Only variables of the appropriate type can be stored in an array.
 You cannot have an array that will store both ints and Strings, for
  instance.
 Like all other variables in Java an array must be declared.
 When you declare an array variable you suffix the type with [] to
  indicate that this variable is an array. Here are some examples:
   int[] k;
   float[] yt;
   String[] names;
Allocating Arrays
 Declaring an array merely says what it is. It does not create the array.
 To actually create the array (or any other object) use the new operator.
 When we create an array we need to tell the compiler how many
 elements will be stored in it.
 Here's how we'd create the variables declared above
   new k = new int[3];
     yt = new float[7];
     names = new String[50];
 The numbers in the brackets specify the dimension of the array; i.e. how
  many slots it has to hold values. With the dimensions above k can hold
  three ints, yt can hold seven floats and names can hold fifty Strings. 
Initializing Arrays
 Individual elements of the array are referenced by the array name and by an
  integer which represents their position in the array.
 The numbers we use to identify them are called subscripts or indexes into the
  array.
 Subscripts are consecutive integers beginning with 0.
 Thus the array k above has elements k[0], k[1], and k[2].
 Since we started counting at zero there is no k[3], and trying to access it will
  generate an ArrayIndexOutOfBoundsException.
 Here's how we'd store values in the arrays we've been working with:
 k[0] = 2;
  k[1] = 5;
  k[2] = -2;
  yt[6] = 7.5f;
  names[4] = "Fred";
Loop for initialization
For even medium sized arrays, it's unwieldy to specify each
 element individually. It is often helpful to use for loops to initialize
 the array. For instance here is a loop that fills an array with the
 squares of the numbers from 0 to 100.
float[] squares = new float[101];

  for (int i=0; i <= 100; i++)
 {
    squares[i] = i*2;
  }
Shortcuts
We can declare and allocate an array at the same time like this:
  int[] k = new int[3];
    float[] yt = new float[7];
    String[] names = new String[50];

We can even declare, allocate, and initialize an array at the same
  time providing a list of the initial values inside brackets like so:
  int[] k = {1, 2, 3};
    float[] yt = {0.0f, 1.2f, 3.4f, -9.87f, 65.4f, 0.0f, 567.9f};
Passing Array to functions
public class test
{
       public static void main(String [] args)
       {
               int [] x = {1,2,3};
               print(x);
       }
       static void print(int [] y)
       {
               for(int i=0;i<3;i++)
               {
                        System.out.println(y[i]);
               }
       }
}
two-
Dimensional
  arrays
Two-dimensional array
Array of arrays
Declaring, Allocating and Initializing Two
Dimensional Arrays
 Two dimensional arrays are declared, allocated and initialized much like
 one dimensional arrays.
 However we have to specify two dimensions rather than one, and we
 typically use two nested for loops to fill the array.
      int[][] M;
       M = new int[4][5];
 for (int row=0; row < 4; row++)
    {
          for (int col=0; col < 5; col++)
         {
            M[row][col] = row+col;
          }
}
Memory layout
Example of filled matrix
multiDimensional
    arrays
A three-dimensional array (3 x 5 x 4)
view of three-dimensional array
Multi Dimensional Array
Java lets you have arrays of three, four or more dimensions.
However chances are pretty good that if you need more than three
  dimensions in an array, you're probably using the wrong data
  structure.
Syntax of three dimensional Arrays
The syntax for three dimensional arrays is a direct extension of that
  for two-dimensional arrays. Here's a program that declares,
  allocates and initializes a three-dimensional array:
      int[][][] M;
      M = new int[4][5][3];
      for (int row=0; row < 4; row++) {
        for (int col=0; col < 5; col++) {
          for (int ver=0; ver < 3; ver++) {
            M[row][col][ver] = row+col+ver;
          }
        }
      }
      

Contenu connexe

Tendances (20)

Array
ArrayArray
Array
 
Array
ArrayArray
Array
 
Array and string
Array and stringArray and string
Array and string
 
Computer programming 2 Lesson 13
Computer programming 2  Lesson 13Computer programming 2  Lesson 13
Computer programming 2 Lesson 13
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
Arrays Basics
Arrays BasicsArrays Basics
Arrays Basics
 
Array
ArrayArray
Array
 
C# Arrays
C# ArraysC# Arrays
C# Arrays
 
Array in c++
Array in c++Array in c++
Array in c++
 
Arrays
ArraysArrays
Arrays
 
Array
ArrayArray
Array
 
arrays of structures
arrays of structuresarrays of structures
arrays of structures
 
Basic data structures in python
Basic data structures in pythonBasic data structures in python
Basic data structures in python
 
Chap 7(array)
Chap 7(array)Chap 7(array)
Chap 7(array)
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
Array
ArrayArray
Array
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Data structure array
Data structure  arrayData structure  array
Data structure array
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Arrays
ArraysArrays
Arrays
 

En vedette

Case studys of RockSound and Kerrang!
Case studys of RockSound and Kerrang!Case studys of RockSound and Kerrang!
Case studys of RockSound and Kerrang!Chloe Main
 

En vedette (8)

Comp102 lec 4
Comp102   lec 4Comp102   lec 4
Comp102 lec 4
 
Comp102 lec 5.1
Comp102   lec 5.1Comp102   lec 5.1
Comp102 lec 5.1
 
Comp102 lec 7
Comp102   lec 7Comp102   lec 7
Comp102 lec 7
 
Case studys of RockSound and Kerrang!
Case studys of RockSound and Kerrang!Case studys of RockSound and Kerrang!
Case studys of RockSound and Kerrang!
 
Comp102 lec 10
Comp102   lec 10Comp102   lec 10
Comp102 lec 10
 
Comp102 lec 5.0
Comp102   lec 5.0Comp102   lec 5.0
Comp102 lec 5.0
 
Comp102 lec 9
Comp102   lec 9Comp102   lec 9
Comp102 lec 9
 
Comp102 lec 3
Comp102   lec 3Comp102   lec 3
Comp102 lec 3
 

Similaire à Comp102 lec 8 (20)

Arrays
ArraysArrays
Arrays
 
Array
ArrayArray
Array
 
Programming in c arrays
Programming in c   arraysProgramming in c   arrays
Programming in c arrays
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 
Arrays
ArraysArrays
Arrays
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
 
Arrays
ArraysArrays
Arrays
 
Array and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdfArray and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdf
 
Arrays
ArraysArrays
Arrays
 
ARRAYS
ARRAYSARRAYS
ARRAYS
 
Array.pptx
Array.pptxArray.pptx
Array.pptx
 
2 arrays
2   arrays2   arrays
2 arrays
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
 
Arrays are used to store multiple values in a single variable, instead of dec...
Arrays are used to store multiple values in a single variable, instead of dec...Arrays are used to store multiple values in a single variable, instead of dec...
Arrays are used to store multiple values in a single variable, instead of dec...
 
Algo>Arrays
Algo>ArraysAlgo>Arrays
Algo>Arrays
 
Unit ii data structure-converted
Unit  ii data structure-convertedUnit  ii data structure-converted
Unit ii data structure-converted
 
Arrays in C Programming
Arrays in C ProgrammingArrays in C Programming
Arrays in C Programming
 
Java arrays (1)
Java arrays (1)Java arrays (1)
Java arrays (1)
 

Comp102 lec 8

  • 5. An array of scores
  • 6. Arrays Arrays are generally effective means of storing groups of variables. An array is a group of variables that share the same name and are ordered sequentially from zero to one less than the number of variables in the array. The number of variables that can be stored in an array is called the array's dimension. Each variable in the array is called an element of the array.
  • 8. Creating Arrays There are three steps to creating an array, declaring it, allocating it and initializing it.
  • 9. Declaring Arrays  Like other variables in Java, an array must have a specific type like byte, int, String or double.  Only variables of the appropriate type can be stored in an array.  You cannot have an array that will store both ints and Strings, for instance.  Like all other variables in Java an array must be declared.  When you declare an array variable you suffix the type with [] to indicate that this variable is an array. Here are some examples: int[] k; float[] yt; String[] names;
  • 10. Allocating Arrays  Declaring an array merely says what it is. It does not create the array.  To actually create the array (or any other object) use the new operator.  When we create an array we need to tell the compiler how many elements will be stored in it.  Here's how we'd create the variables declared above  new k = new int[3]; yt = new float[7]; names = new String[50];  The numbers in the brackets specify the dimension of the array; i.e. how many slots it has to hold values. With the dimensions above k can hold three ints, yt can hold seven floats and names can hold fifty Strings. 
  • 11. Initializing Arrays  Individual elements of the array are referenced by the array name and by an integer which represents their position in the array.  The numbers we use to identify them are called subscripts or indexes into the array.  Subscripts are consecutive integers beginning with 0.  Thus the array k above has elements k[0], k[1], and k[2].  Since we started counting at zero there is no k[3], and trying to access it will generate an ArrayIndexOutOfBoundsException.  Here's how we'd store values in the arrays we've been working with:  k[0] = 2; k[1] = 5; k[2] = -2; yt[6] = 7.5f; names[4] = "Fred";
  • 12. Loop for initialization For even medium sized arrays, it's unwieldy to specify each element individually. It is often helpful to use for loops to initialize the array. For instance here is a loop that fills an array with the squares of the numbers from 0 to 100. float[] squares = new float[101]; for (int i=0; i <= 100; i++) {   squares[i] = i*2; }
  • 13. Shortcuts We can declare and allocate an array at the same time like this: int[] k = new int[3]; float[] yt = new float[7]; String[] names = new String[50]; We can even declare, allocate, and initialize an array at the same time providing a list of the initial values inside brackets like so: int[] k = {1, 2, 3}; float[] yt = {0.0f, 1.2f, 3.4f, -9.87f, 65.4f, 0.0f, 567.9f};
  • 14. Passing Array to functions public class test { public static void main(String [] args) { int [] x = {1,2,3}; print(x); } static void print(int [] y) { for(int i=0;i<3;i++) { System.out.println(y[i]); } } }
  • 18. Declaring, Allocating and Initializing Two Dimensional Arrays  Two dimensional arrays are declared, allocated and initialized much like one dimensional arrays.  However we have to specify two dimensions rather than one, and we typically use two nested for loops to fill the array.  int[][] M; M = new int[4][5];  for (int row=0; row < 4; row++) {       for (int col=0; col < 5; col++) {         M[row][col] = row+col;       } }
  • 24. Multi Dimensional Array Java lets you have arrays of three, four or more dimensions. However chances are pretty good that if you need more than three dimensions in an array, you're probably using the wrong data structure.
  • 25. Syntax of three dimensional Arrays The syntax for three dimensional arrays is a direct extension of that for two-dimensional arrays. Here's a program that declares, allocates and initializes a three-dimensional array:     int[][][] M;     M = new int[4][5][3];     for (int row=0; row < 4; row++) {       for (int col=0; col < 5; col++) {         for (int ver=0; ver < 3; ver++) {           M[row][col][ver] = row+col+ver;         }       }     }