How to Pass an Array to Constructor

How to pass an array to constructor?

First, usually class names in Java starts with Upper case, now, to the problem you met, it should be:

public class TargetClass { /* Attributes */ 
private byte[][] array;

/* Constructor */
public TargetClass (byte[][] array) {
this.array = array;
}
}

how to pass array to constructor in c++?

Your example is working fine - just take care to delete[] with new[] allocated space accordingly after you're finished (to prevent memory leaking). Also instead of using 15 as hardcoded constant, you probably want to use the parameter size (otherwise for arrays bigger than 15 elements, you would have an memory access violation).

class ArrayClass{
public:
ArrayClass(int* array, int size){
a = new int[size];
for (int i(0); i < size; i++) {
this->a[i] = array[i];
}
this->size = size;
}
virtual ~ArrayClass(){
delete[] a;
}
private:
int* a;
int size;
};

int main(int argc, char** argv){
int array[3] = { 1, 2, 3 };
ArrayClass a2(array, 3);
return 0;
}

Arrays can be allocated in C++ in different ways:

int a[3]; // will allocate the space for 3 elements statically from the stack

int* a = new int[3]; // will allocate space for 3 elements dynamically from the heap

Basically it decides in which memory your array will be located - but there are many differences in these two methods involved - see i.e. What and where are the stack and heap?.
Some main differences are:

  • stack allocations can't use a dynamical length like a variable i.e. int size = 10; int a[size]; // <- is invalid
  • stack allocations are 'deleted' automatically when they are out-of-scope
  • heap allocations have to be deleted[] explicit to not leak memory

The line int* a = new int[3]; will declare a pointer-variable to an memory location where 3 int values can fit in. So after the declaration these 3 values can be addressed directly as a[0], a[1] and a[2]. After all operations are finished delete[] a; is necessary. Because if the pointer a* is getting out-of-scope (i.e. end of your function) the memory for the 3 values is not deallocated automatically.

How to pass a array to a constructor and use it in a class?

You declared your array double[] tScore = new double[size]; to be of zero size. You intended to replace it with a new array in your constructor here:

double[] tScore = scores;

However, all you did is declare a local variable of the same name, and assign the parameter to it, which immediately goes out of scope at the end of the constructor. The for loop appears not to be entered, because tScore is still empty and of zero length.

Remove double[] so that tScore resolves to your instance variable and get assigned properly.

tScore = scores;

Also, it's confusing to see a parameter named sizE and an instance variable named size. It's common to use this to refer to the instance variable, so that they can be named the same name and yet be clear. Try naming the parameter size also, and use this line in your constructor:

this.size = size;

How to pass array elements in constructor?

You need to pass your int[] array into the Student constructor, not build the array inside it.

I'd swap this line

private Array[] obj =new Array[5];

for

private int[] grades = new int[5];

In your main method, build your grades for your new student object int[] ssGrades = new int[5]; Then assign your values as you have in your student constructor below that so ssGrades[0] = 9 etc.

In your student constructor, replace

Student(String Name, int Array[])

for

Student(String name, int[] grades);

because you want to send your constructor an integer array, not an int called Array[]. Then pass this into the Student constructor:

Student ss = new Student(name, ssGrades[]);

Then in your student class you want to say that this.Name = Name as you have done, but then

this.grades = grades;

I think you need to separate in your head the difference between an object that's Array[], and object that's int[] varName as well as what is a property being assigned to an object and the object itself.

Your problems have come from your code naming variables in a way that makes it confusing for you to use, in my opinion.

Also, you don't need a for loop when you're not using the index of the for loop. All that

for(int i=0; i < Array.length;i++){
Array[0] = 9;
etc
}

is doing the filling of your array with the same values (Array.length) amount of times (because there's no use of your index i, nor can you use it because the grades you're trying to input in are not dependent on i).
I hope this answer helps.

Passing an object array to a constructor

The syntax for inline array creation is new ArrayType[]{elem1, elemn2}

so

Book theIdiot = new Book("The Idiot", new Author[]{author2, author3}, 44.5, 33);

Or the not inlined version

Author[] authors = new Author[2];
authors[0] = author2;
authors[1] = author3;
Book theIdiot = new Book("The Idiot", authors, 44.5, 33);

Or as "Roddy of the Frozen Peas" mentioned in the comment

Author[] authors = {author2, author3}
Book theIdiot = new Book("The Idiot", authors, 44.5, 33);

How to pass an array as a parameter in a class constructor in PHP

Add:

$this->tableFields = $tableFields;

And:

$myObject = new GenerateCrud('users_test', 'id', ['field1', 'field2', 'field3', 'field4']);

How to pass an array list to the constructor within loop

At first, you have loop error, in you case "for-loop" will be complete only 3 times, if you would like 4 times, it's should be like this:

for(int i = 0; i < 4; i++){
constructor(); // this is where I need your help, how to pass value to them
}

About your question, you can do it like this:

List<String> arguments = [
"arg01",
"arg02",
"arg03",
"arg04",
"arg05",
"arg06",
"arg07",
"arg08",
"arg09",
"arg10",
"arg11",
"arg12",
"arg13",
"arg14",
"arg15",
"arg16"
];

for (int i = 0; i < 16; i += 4) {
constructor(arguments[i], arguments[i + 1], arguments[i + 2], arguments[i + 3]);
}

More flexible solution is:

List<String> arguments = [
"arg01",
"arg02",
"arg03",
"arg04",
"arg05",
"arg06",
"arg07",
"arg08",
"arg09",
"arg10",
"arg11",
"arg12",
"arg13",
"arg14",
"arg15",
"arg16"
];

// Number of arguments in constructor
int argsNumber = 4;

for (int i = 0; i < arguments.length; i += argsNumber) {
constructor(arguments[i], arguments[i + 1], arguments[i + 2], arguments[i + 3]);
}

If you have different types of arguments, better solution would be to create entity-class(object) to contain arguments, for example:

void main() {
// List with objects
List<Item> items = [
Item("Name1", 23, 80.2),
Item("Name2", 32, 77.1),
Item("Name3", 54, 78.8),
Item("Name4", 18, 67.5),
];

// Call constructor for each object
items.forEach((it) => constructor(it));
}

// Object to contain arguments
class Item {
String name;
int age;
double weight;

Item(this.name, this.age, this.weight);
}


Related Topics



Leave a reply



Submit