Java Tutorial/Collections/Multidimensional Arrays

Материал из Java эксперт
Перейти к: навигация, поиск

Array Of int Arrays

/*
 * Copyright (c) 1995 - 2008 Sun Microsystems, Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Sun Microsystems nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
public class ArrayOfArraysDemo2 {
  public static void main(String[] args) {
    int[][] aMatrix = new int[4][];
    // populate matrix
    for (int i = 0; i < aMatrix.length; i++) {
      aMatrix[i] = new int[5]; // create sub-array
      for (int j = 0; j < aMatrix[i].length; j++) {
        aMatrix[i][j] = i + j;
      }
    }
    // print matrix
    for (int i = 0; i < aMatrix.length; i++) {
      for (int j = 0; j < aMatrix[i].length; j++) {
        System.out.print(aMatrix[i][j] + " ");
      }
      System.out.println();
    }
  }
}





Array Of string Arrays

/*
 * Copyright (c) 1995 - 2008 Sun Microsystems, Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Sun Microsystems nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
public class ArrayOfArraysDemo {
  public static void main(String[] args) {
    String[][] cartoons = {
        { "Flintstones", "Fred", "Wilma", "Pebbles", "Dino" },
        { "Rubbles", "Barney", "Betty", "Bam Bam" },
        { "Jetsons", "George", "Jane", "Elroy", "Judy", "Rosie", "Astro" },
        { "Scooby Doo Gang", "Scooby Doo", "Shaggy", "Velma", "Fred", "Daphne" } };
    for (int i = 0; i < cartoons.length; i++) {
      System.out.print(cartoons[i][0] + ": ");
      for (int j = 1; j < cartoons[i].length; j++) {
        System.out.print(cartoons[i][j] + " ");
      }
      System.out.println();
    }
  }
}





Arrays of Arrays

public class MainClass public class MainClass {
  public static void main(String[] args) {
    float[][] t = new float[10][365]; // Temperature array
    for (int i = 0; i < t.length; i++) {
      for (int j = 0; j < t[i].length; j++) {
        t[i][j] = (float) (45.0 * Math.random() - 10.0);
      }
    }
    float average = 0.0f;
    for (int i = 0; i < t.length; i++) {
      for (int j = 0; j < t[i].length; j++) {
        average += t[i][j];
      }
    }
    System.out.println(average);
  }
}



44505.707


Arrays of Arrays in Varying Length

public class MainClass {
  public static void main(String[] arg) {
    float[][] samples = new float[6][]; // Define 6 elements,
    // each is an array
    for (int i = 0; i < samples.length; i++) {
      samples[i] = new float[i + 1]; // Allocate each array
    }
    
    for (int i = 0; i < samples.length; i++) {
      System.out.println(samples[i].length);
    }
    
  }
}



1
2
3
4
5
6


Defining Multidimensional Arrays

public class MainClass {
  public static void main(String[] arg) {
    long[][][] beans = new long[5][10][30];
    for (int i = 0; i < beans.length; i++) { 
      for (int j = 0; j < beans[i].length; j++) {
        beans[i][j] = new long[(int) (1.0 + 6.0 * Math.random())];
      }
    }
  }
}





Get array upperbound

public class Main {
  public static void main(String args[]) {
    String[][] data = new String[3][4];
    System.out.println("Dimension 1: " + data.length);
    System.out.println("Dimension 2: " + data[0].length);
  }
}
/*
Dimension 1: 3
Dimension 2: 4
*/





Initialize a two-dimensional array in matrix

public class MainClass {
  public static void main(String args[]) {
    double m[][] = {
      { 0*0, 1*0, 2*0, 3*0 },
      { 0*1, 1*1, 2*1, 3*1 },
      { 0*2, 1*2, 2*2, 3*2 },
      { 0*3, 1*3, 2*3, 3*3 }
    };
    int i, j;
   
    for(i=0; i<4; i++) {
      for(j=0; j<4; j++)
        System.out.print(m[i][j] + " ");
      System.out.println();
    }
  }
}



0.0 0.0 0.0 0.0 
0.0 1.0 2.0 3.0 
0.0 2.0 4.0 6.0 
0.0 3.0 6.0 9.0


To get the number of dimensions

public class Main {
  public static void main(String args[]) {
    String[][] data = new String[3][4];
    System.out.println(getDimension(data));
  }
  public static int getDimension(Object array) {
    int dim = 0;
    Class c = array.getClass();
    while (c.isArray()) {
      c = c.getComponentType();
      dim++;
    }
    return (dim);
  }
}
//2