-1

In the below pic contains the structure of 3D array that I want to create

I have 5 Flight with numbers 777,747,777,747 and 777.

I want to create a 2D array with size 5*3 if the flight = 777 and a 2D array with size 10*5 if flight = 747.

Structure of Flight 3D array

int seat[][][] = new int [5][][];
int flight = 0;
while (flight < 5) {
    if (Boeing_Number[flight] == 777) {
        seat = new int[flight][5][3];
    } 
    else if (Boeing_Number[flight] == 747) {
        seat = new int[flight][10][5];
        System.out.println(" created ");
    }
    flight++;
}
thor
  • 21,418
  • 31
  • 87
  • 173
Mê D Ôo
  • 33
  • 1
  • 1
  • 6
  • 2
    Possible duplicate of [Initialising a multidimensional array in Java](http://stackoverflow.com/questions/1067073/initialising-a-multidimensional-array-in-java) – Nico Van Belle Mar 06 '17 at 11:38

3 Answers3

0

Maybe you can define instead two clasess:

  • A class Flight777 which has a 2D arrays of size 5x3 as an attribute
  • A class Flight747 which has a 2D array of size 10x5 as an attribute

This doesn't answer your question of the 3D arrays, but I think it can be a better solution to tackle the problem.

Ignacio Alorre
  • 7,307
  • 8
  • 57
  • 94
  • It's a terrible solution to make classes for specific instances. The only acceptable thing would be a Flight class and instances of that. – Silverclaw Mar 09 '17 at 07:27
  • He was making several instances of each class (3 of the 777 and 2 of 747). Yeah he could make a flight class, and then subclases for each type of flight, because each type of classes needs a different kind of array. What is the problem about that? – Ignacio Alorre Mar 09 '17 at 07:29
0

If you want to create

  • 2D array of size 5*3 (if flight = 777)
  • 2D array of size 10*5 (if flight = 747)

and set into respective index of seat (where index is defined by variable flight):

int seat[][][] = new int [5][][];
int flight = 0;
while (flight < 5) {
    if (Boeing_Number[flight] == 777) {
        seat[flight] = new int[5][3];
    } 
    else if (Boeing_Number[flight] == 747) {
        seat[flight] = new int[10][5];
    }
    flight++;
}
sazzad
  • 5,740
  • 6
  • 25
  • 42
-1
import java.util.Scanner;

public class ThreeDArray

{

 public static void main(String []args)

 {

  Scanner s=new Scanner(System.in);

  System.out.println("Enter number of Pages:");

  int p=s.nextInt();

  System.out.println("Enter number of rows:");

  int r=s.nextInt();

  System.out.println("Enter number of colums:");

  int c=s.nextInt();

  int arr[][][]=new int[p][r][c];

  System.out.println("Enter numbers in Three dimentional Array:");

   for(int j=0;j<arr.length;j++)

  for(int i=0;i<arr.length;i++)

  for(int k=0;k<arr.length;k++)

  {

   arr[i][j][k]=s.nextInt();

  }

  System.out.println("printing");

  for(int j=0;j<arr.length;j++)

  for(int i=0;i<arr.length;i++)

  for(int k=0;k<arr.length;k++)

  {

   System.out.println(arr[i][j][k]);

  }

 }

}
ALI KHAN
  • 39
  • 5
  • Hi, welcome to Stack Overflow. When answering a question that already has many answers, please be sure to add some additional insight into why the response you're providing is substantive and not simply echoing what's already been vetted by the original poster. This is especially important in "code-only" answers such as the one you've provided. In this case, you've copied and pasted from a blog, without explanation. – chb Mar 13 '19 at 22:05