Wednesday, November 20, 2024
Google search engine
HomeData Modelling & AIGet value from Multidimentional Array containing an Object

Get value from Multidimentional Array containing an Object

To get a value from a multidimensional array containing objects in C++, you can use the same syntax as you would for a regular multidimensional array, with the added step of accessing the appropriate member variable of the object. Simply use the appropriate index notation to access the object in the array, and then use the dot operator “.” to access the desired member variable or function.

Here’s an example code snippet that demonstrates how to get a value from a two-dimensional array containing objects:

C++




// C++ Implementation
#include <iostream>
#include <string>
using namespace std;
 
// Create a class
class MyClass {
public:
    int value;
 
    MyClass(int val) { value = val; }
};
 
// Driver code
int main()
{
 
    // Create a 2D array of objects
    MyClass arr[2][2] = { { MyClass(1), MyClass(2) },
                          { MyClass(3), MyClass(4) } };
 
    // Access a value in the array
    int val = arr[1][0].value;
 
    // print the value
    cout << "Value at index [1][0] is: " << val << endl;
 
    return 0;
}


Java




import java.io.*;
 
// Create a class
class MyClass {
    public int value;
 
    public MyClass(int val) {
        value = val;
    }
}
 
// Driver code
class Main {
    public static void main(String[] args) {
        // Create a 2D array of objects
        MyClass[][] arr = { { new MyClass(1), new MyClass(2) },
                            { new MyClass(3), new MyClass(4) } };
 
        // Access a value in the array
        int val = arr[1][0].value;
 
        // print the value
        System.out.println("Value at index [1][0] is: " + val);
    }
}


Python3




# Python Implementation
 
# Create a class
 
 
class MyClass:
    def __init__(self, val):
        self.value = val
 
 
# Driver code
if __name__ == '__main__':
    # Create a 2D array of objects
    arr = [[MyClass(1), MyClass(2)],
           [MyClass(3), MyClass(4)]]
 
    # Access a value in the array
    val = arr[1][0].value
 
    # Print the value
    print(f"Value at index [1][0] is: {val}")


C#




using System;
 
// Create a class
class MyClass
{
    public int value;
 
    public MyClass(int val)
    {
        value = val;
    }
}
 
class MainClass
{
    public static void Main(string[] args)
    {
        // Create a 2D array of objects
        MyClass[,] arr = { { new MyClass(1), new MyClass(2) },
                           { new MyClass(3), new MyClass(4) } };
 
        // Access a value in the array
        int val = arr[1, 0].value;
 
        // print the value
        Console.WriteLine("Value at index [1, 0] is: " + val);
    }
}


Javascript




// JavaScript implementation
 
// Create a class
class MyClass {
constructor(val) {
this.value = val;
}
}
 
// Driver code
 
// Create a 2D array of objects
let arr = [[new MyClass(1), new MyClass(2)],
[new MyClass(3), new MyClass(4)]];
 
// Access a value in the array
let val = arr[1][0].value;
 
// Print the value
console.log('Value at index [1][0] is: ' + val);


Output

Value at index [1][0] is: 3



In this example, we define a class MyClass with a single member variable value and use it to create a two-dimensional array arr of size 2×2. We then access the value at index [1][0] of the array by using the dot operator. to access the value member variable of the object at that index.

Here’s another example of how to get a value from a multidimensional array containing objects in C++:

C++




// C++ implementation
#include <iostream>
#include <string>
 
using namespace std;
 
// Define a class called "Student"
// to represent a student
class Student {
public:
    string name;
    int age;
    double gpa;
 
    // Constructor to initialize the
    // values of the student
    Student(string n, int a, double g)
    {
        name = n;
        age = a;
        gpa = g;
    }
};
 
// Drivers code
int main()
{
 
    // Create a 2D array
    // of student objects
    Student students[2][2]
        = { { Student("Alice", 18, 3.8),
              Student("Bob", 20, 3.5) },
            { Student("Charlie", 19, 3.7),
              Student("Dave", 21, 3.9) } };
 
    // Access a value in the array
    double gpa = students[0][1].gpa;
 
    // Print the value
    cout << "GPA of student at index [0][1] is: " << gpa
         << endl;
 
    return 0;
}


Java




// Java implementation
public class Student {
    public String name;
    public int age;
    public double gpa;
 
    // Constructor to initialize the values of the student
    public Student(String n, int a, double g)
    {
        name = n;
        age = a;
        gpa = g;
    }
 
    // Main method
    public static void main(String[] args)
    {
        // Create a 2D array of student objects
        Student[][] students
            = { { new Student("Alice", 18, 3.8),
                  new Student("Bob", 20, 3.5) },
                { new Student("Charlie", 19, 3.7),
                  new Student("Dave", 21, 3.9) } };
 
        // Access a value in the array
        double gpa = students[0][1].gpa;
 
        // Print the value
        System.out.println(
            "GPA of student at index [0][1] is: " + gpa);
    }
}


Python3




# Define a class called "Student"
# to represent a student
class Student:
    def __init__(self, n, a, g):
        self.name = n
        self.age = a
        self.gpa = g
 
# Create a 2D array
# of student objects
students = [[Student("Alice", 18, 3.8),
             Student("Bob", 20, 3.5)],
            [Student("Charlie", 19, 3.7),
             Student("Dave", 21, 3.9)]]
 
# Access a value in the array
gpa = students[0][1].gpa
 
# Print the value
print(f"GPA of student at index [0][1] is: {gpa}")


C#




// C# implementation
 
using System;
 
// Define a class called "Student"
// to represent a student
class Student
{
    public string name;
    public int age;
    public double gpa;
 
    // Constructor to initialize the
    // values of the student
    public Student(string n, int a, double g)
    {
        name = n;
        age = a;
        gpa = g;
    }
}
 
// Drivers code
class Program
{
    static void Main(string[] args)
    {
        // Create a 2D array
        // of student objects
        Student[,] students = new Student[2, 2]
        {
            { new Student("Alice", 18, 3.8),
              new Student("Bob", 20, 3.5) },
            { new Student("Charlie", 19, 3.7),
              new Student("Dave", 21, 3.9) }
        };
 
        // Access a value in the array
        double gpa = students[0, 1].gpa;
 
        // Print the value
        Console.WriteLine("GPA of student at index [0, 1] is: " + gpa);
 
        Console.ReadLine();
    }
}


Javascript




// Define a class called "Student"
// to represent a student
class Student {
    // Constructor to initialize the
    // values of the student
    constructor(n, a, g) {
        this.name = n;
        this.age = a;
        this.gpa = g;
    }
}
 
// Drivers code
 
// Create a 2D array
    // of student objects
const students = [
    [new Student("Alice", 18, 3.8), new Student("Bob", 20, 3.5)],
    [new Student("Charlie", 19, 3.7), new Student("Dave", 21, 3.9)]
];
 
// Access a value in the array
const gpa = students[0][1].gpa;
 
console.log("GPA of student at index [0][1] is: " + gpa);


Output

GPA of student at index [0][1] is: 3.5



Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Last Updated :
20 Sep, 2023
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

RELATED ARTICLES

Most Popular

Recent Comments