How to create an array in constructor that will be used later

java create array of objects with constructor
how to create an array in java
how to declare empty array in java
array constructor c++
string array declaration in java
valid array declaration in java
initialize array java
1d array java

I am making a Matrix class and want to make the constructor input the Matrix type too. How can I initialize a specific type 2d array?

public class Matrix
    {
        public int[][] matrix;
        //??


        Matrix(int x,int y,string type)
        {
            switch (type)
            {
                case "int":
                    //initialize a int 2d array
                case "double":
                    //initialize a double 2d array
                case "float":
                    //initialize a float 2d array
                default:
                   //initialize a float 2d array
                   break;

           }

       }
   }

Generics with a constraint of new might help, if the type can be declared at design time

public class Matrix<T> where T : new()
{
   public T[][] matrix;
   public Matrix(int x, int y)
      => matrix = Enumerable.Range(0,x).Select(elem => new T[y]).ToArray();    
}

Usage

var something = new Matrix<int>(4,4);

Array Of Objects In Java: How To Create, Initialize And Use, So, if you have a class 'Employee' then you can create an array of above will need to be instantiated using 'new' before being used in the program. One way to initialize the array of objects is by using the constructors. Arrays can be created using a constructor with a single number parameter. An array with its length property set to that number and the array elements are empty slots. let fruits = new Array(2); console.log(fruits.length); // 2 console.log(fruits[0]); // undefined.

Standard approach would be to use generics instead of string name of the type like Matrix<T>.

If you want to specify type as string at run-time you have to either add multiple fields to store each type of an array or use dynamic or object as the type of array (since you can't assign float[][] to field of type int[][]).

For non-performance sensitive code I'd go with dynamic as it will give you code that looks reasonable (you loose intellisense and compile time safety) and validates all operations at run-time anyway.

public class Matrix
{
    dynamic matrix;

    Matrix(int x,int y,string type)
    {
        switch (type)
        {
            case "int":
                matrix = 
                   Enumerable.Repeat(0, x).Select(_ => new int[y]).ToArray();
                break;
            case "double":
                //initialize a double 2d array
                matrix = 
                   Enumerable.Repeat(0, x).Select(_ => new double[y]).ToArray();
                break;
            case "float":
            default:
                //initialize a float 2d array
                matrix = 
                   Enumerable.Repeat(0, x).Select(_ => new float[y]).ToArray();
                break;
         }
     }
 }

For array initialization see Multidimensional Array question.

Creating and Using Arrays, The sample program uses int[] , so the array called anArray will be used to hold integer data. Here are declarations for arrays that hold other types of data:. Constructor Reference for Arrays. Another form of the constructor reference syntax that is used for arrays. To create a constructor reference for an array, use the construct : type[ ]::new. Type specifies the type of object being created. For example, the following creates a three-element array of MyClass objects and gives each element an

Look into generics

public class Matrix<T> 
{    
    public T[,] matrix;

    public Matrix<T>(int x, int y)
    {
        matrix = new T[x, y];
    }
}

You can probably find an existing, well-tested Matrix class somewhere on the internet.

How to initialize Array of objects with parameterized constructors in , “malloc” or “memory allocation” method in C++ is used to dynamically allocate a single large block of memory with the specified size. It returns a pointer of type� You can declare and instantiate the array of objects as shown below: Employee[] empObjects = new Employee[2]; Note that once an array of objects is instantiated like above, the individual elements of the array of objects need to be created using new. The above statement will create an array of objects ‘empObjects’ with 2 elements/object references.

Array() constructor, The Array() constructor is used to create Array objects. Syntax. [ element0 , element1 , , elementN ] new Array( element0 , element1 [, . Array elements have default values of 0/false/null, depending on the element type. So you have 3 main choices: 1. Create an array of only 3 elements. 2. Initialize all 10 elements. 3. Check for array[i] != null before printing it. Note that #1 and #2 are really the same thing--don't create your array any bigger than you need.

Arrays in Java, They will be described later in a separate chapter. The square brackets are used for declaring an array, the type ( int in our case) tells us what type of values � One would be to create an array of pointers, instead, and the create the instances in the WidgetArray constructor, and point to them. The other is to create a no-argument constructor for WidgetI2C, and add a method that sets the value that the one-argument constructor sets. ksmiller99

[PDF] Chapter 8 Topics Arrays Declaring and Instantiating Arrays , Arrays are objects, so creating an array requires two using an initialization list after the array is instantiated will generate a compiler error. • The new keyword is not used when an array is A constructor (or mutator) that accepts an array as. Since array definitions are created by the compiler implicitly, there is no way for you to create a specific "constructor" -- you will have to create a method, that will take an array and create a new instance.

Comments
  • Same answer as Michael's, except you are showing some other array type than OP asked for - sample code shows Jagged Array (which they probably by mistake call 2d array)