answersLogoWhite

0


Best Answer

An (non generic) arrayList in java can save any type of object (in this case your class variable) in this straightforward way:

MyClass myClassVar = new MyClass();

ArrayList myArrayList = new ArrayList();

myArrayList.add(myClassVar);

User Avatar

Wiki User

13y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How you can store a class variables in array list?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Can you store non primitive data type in the array list?

Yes you can store non primitive data type variables in an array. String is a non primitive data type. You can declare a string array as: String a[]=new String[10];


What is difference between array and class?

An array stores a list of values, usually of the same type. Values are distinguished by a subscript. A class usually doesn't store objects, but it defines a template for objects. Objects based on this class store data, which can be of different types, and are distinguished by name (similar to variables). A class thus defines a new, composite, data type (so does an array, but in a more limited way). Besides, a class can also define functions (usually called "methods"), specifically for objects of that type. Usage example: If you want to sort 100 numbers, or do some other work with them, you might store them in an array of numbers (integers, or floating-point numbers, depending on your needs). If, on the other hand, you want to store information about a person, you might create a class, where you define that a "person" includes information about a first name, last name, birthdate, etc.


Can you change the size of an array dynamically in Java?

No, you can't change the size of an array dynamically. If you are needing to change the size of an array dynamically use an ArrayList, LinkedList, ConcurrrentHashMap, or another class that meets your needs.


Create a mark list using array with structure?

sorce code for student mark list usig array


Can Array-based lists can never be empty?

It's either an array or it's a list, it cannot be both. However, an empty array is entirely possible: std::vector<int> my_vector; // an empty array my_vector.push_back(42); // an array of 1 element my_vector.push_back(1); // an array of 2 elements my_vector.clear(); // an empty array An empty list is also possible: std::list<int> my_list; // an empty list my_list.push_back(42); // a list of 1 element my_list.push_back(1); // a list of 2 elements my_list.clear(); // an empty list The same thing can be done in C: int* my_array = nullptr; // an empty array my_array = malloc (2*sizeof(int)); // an array of 2 elements my_array[0] = 42; my_array[1] = 1; free my_array; // an empty array my_array = 0;

Related questions

Can you store non primitive data type in the array list?

Yes you can store non primitive data type variables in an array. String is a non primitive data type. You can declare a string array as: String a[]=new String[10];


How would you use the word array in a sentence?

An array is a list of data items or variables of the same type, like a list of numbers or a list of dates or a list of names.


Is there a way to create variables as needed in C based on user input?

Yes. You can store any number of values input at runtime using a variable-length array or any other sequence container such as a list.


A travel agent wants a program to store an alphabetical list of winter holiday destinations State the most efficient way to store lists using a programming language?

The most efficient way to store a list is with an array.


What is difference between array and class?

An array stores a list of values, usually of the same type. Values are distinguished by a subscript. A class usually doesn't store objects, but it defines a template for objects. Objects based on this class store data, which can be of different types, and are distinguished by name (similar to variables). A class thus defines a new, composite, data type (so does an array, but in a more limited way). Besides, a class can also define functions (usually called "methods"), specifically for objects of that type. Usage example: If you want to sort 100 numbers, or do some other work with them, you might store them in an array of numbers (integers, or floating-point numbers, depending on your needs). If, on the other hand, you want to store information about a person, you might create a class, where you define that a "person" includes information about a first name, last name, birthdate, etc.


Can you change the size of an array dynamically in Java?

No, you can't change the size of an array dynamically. If you are needing to change the size of an array dynamically use an ArrayList, LinkedList, ConcurrrentHashMap, or another class that meets your needs.


What are some of the benefits of using an array as opposed to a medium-sized list of variables?

Arrays exist in contiguous memory, so you can use simple pointer arithmetic to access any element by its offset from the start address (the array name is an alias for the start address), and can pass the entire array to functions as a single entity. Lists of variables are not guaranteed to exist in contiguous memory, and cannot be passed to functions as a single entity.


What is array literal in as2?

An array literal is a comma-separated list of the elements of an array. An array literal can be used for initializing the elements of an array.


Create a mark list using array with structure?

sorce code for student mark list usig array


Can linked list represent in array if yes show how insertion and deletions can perform in array representation of inked list?

yes


Can Array-based lists can never be empty?

It's either an array or it's a list, it cannot be both. However, an empty array is entirely possible: std::vector<int> my_vector; // an empty array my_vector.push_back(42); // an array of 1 element my_vector.push_back(1); // an array of 2 elements my_vector.clear(); // an empty array An empty list is also possible: std::list<int> my_list; // an empty list my_list.push_back(42); // a list of 1 element my_list.push_back(1); // a list of 2 elements my_list.clear(); // an empty list The same thing can be done in C: int* my_array = nullptr; // an empty array my_array = malloc (2*sizeof(int)); // an array of 2 elements my_array[0] = 42; my_array[1] = 1; free my_array; // an empty array my_array = 0;


What is array variable?

An array is a variable containing multiple values. Any variable may be used as an array. There is no maximum limit to the size of an array, nor any requirement that member variables be indexed or assigned contiguously. Arrays are zero-based: the first element is indexed with the number 0.