If you wanted to put int,double into your own container class would you do it?

In summary: Use a generic type parameter when creating the list.In summary, the conversation discusses how to store ints, doubles, and strings in a custom container class. Suggestions include defining the size and orientation of the data, using typedefs in C/C++, and utilizing generics in Java. The importance of considering platform dependencies and memory allocation for strings is also mentioned.
  • #1
SpiffyEh
194
0
If you wanted to put ints, doubles, strings into your own container class how would you do it?
 
Technology news on Phys.org
  • #2
It might help if you said which language you are talking about.
 
  • #3
SpiffyEh said:
If you wanted to put ints, doubles, strings into your own container class how would you do it?

Aside from CompuChip's suggestion, you also need to define the size and the orientation of your "words".

My suggestion to you is if you are doing your development in something C/C++, you should typedef your doubles, ints, and strings so that they are always in the same format regardless of the platform.

Typically what happens is that an int is platform dependent. On a 32-bit platform is 32 bits and on a 64-bit platform it is 64-bits. The best way to get around this is to define something like INT32 which is guaranteed to 32 bits regardless of what platform you execute the code on.

Also with strings, are they fixed length or are they allocated on the heap? If they are allocated on the heap, then your string data will not be able to stored in your structure as a TYPE or struct definition. One way around this is to use a large flat area of memory that is the maximum size of a string. Languages like BASIC can do this, and it's very easy to do this with a struct definition in C/C++.
 
  • #4
Sorry, I forgot to mention I am trying to do it in Java
 
  • #5
Then you can use generics. Look into List<A>, ArrayList<T>, Vector<T>, etc.
 

What is a container class?

A container class is a class that is used to store and organize data. It can hold different types of data and provides methods to manipulate and access that data.

Why would I want to put int or double into my own container class?

By creating your own container class, you have control over how the data is stored and accessed. This can be useful when you have a specific way of organizing or manipulating the data that is not provided by existing container classes.

How would I go about putting int or double into my own container class?

To put int or double into your own container class, you would first need to define a class that has the necessary data members and methods to store and manipulate this type of data. You can then use this class as a container for your int or double data.

What are the advantages of using my own container class for int or double data?

Using your own container class allows for more control and customization in how your data is stored and accessed. It also allows for better organization and management of your data, potentially improving efficiency and performance.

Are there any drawbacks to using my own container class for int or double data?

The main drawback of using your own container class is that it requires more time and effort to create and maintain. Additionally, if your class is not well-designed or implemented, it may not perform as efficiently as existing container classes.

Similar threads

  • Programming and Computer Science
2
Replies
36
Views
3K
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
Replies
5
Views
781
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
9
Views
902
  • Programming and Computer Science
Replies
3
Views
1K
Back
Top