473,421 Members | 1,551 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,421 software developers and data experts.

how can i put multiple classtypes in 1 array/arraylist

Ciary
247 Expert 100+
hi,

i have a problem i can't figure out. i'm trying to create a tree structure in a class. to do that i have a class "Knod" and a class "Leaf". the class "Tree" is an ArrayList of arrays. and these arrays contain a boolean, a Knod or Leaf-class and 2 ints (defining the next Knod or Leaf)

Expand|Select|Wrap|Line Numbers
  1. public class Tree{
  2.     private int init;
  3.     private ArrayList<Object[]> boom;
  4.  
  5.     public Tree(){
  6.         this.init = 0;
  7.         this.boom = new ArrayList<Object[]>();
  8.     }
  9.  
  10.     public void newLeaf(double[] waarden){
  11.         Object[] arr = new Object[2];
  12.  
  13.                                 //false = leaf
  14.         arr[0] = new Boolean(false);            //true = Knod
  15.         arr[1] = new Leaf(waarden);
  16.  
  17.         this.boom.add(arr);
  18.     }
  19.  
  20.     public void newKnod(double[] waarden, int trueVal, int falseVal){
  21.         Object[] arr = new Object[4];
  22.  
  23.                                 //false = leaf
  24.         arr[0] = new Boolean(true);            //true = Knod
  25.         arr[1] = new Knod(waarden);
  26.         arr[2] = new Integer(trueVal);
  27.         arr[3] = new Integer(falseVal);
  28.  
  29.         this.boom.add(arr);
  30.     }
  31. }
as you can see i use an array of objects to make this work.
now i want to get the value of the Boolean. usually you would do
Expand|Select|Wrap|Line Numbers
  1. bool.booleanValue()
or in my case
Expand|Select|Wrap|Line Numbers
  1. this.boom.get(id)[0].booleanValue()
but since i made an array of object's he doesn't recognize this function.

so the actual question is, how can i put multiple classtypes in 1 array and still use all the functions?

tnx
Oct 11 '10 #1
6 1846
Dheeraj Joshi
1,123 Expert 1GB
Can't you use vectors or any other collection frame works?

Regards
Dheeraj Joshi
Oct 12 '10 #2
Ciary
247 Expert 100+
please explain that a bit further. as far as i know, a vector and arraylist are the same but from a different language (but i'm probably wrong) Enlighten me
Oct 12 '10 #3
Nepomuk
3,112 Expert 2GB
To come back to the original question, you'll have to cast the Objects. So in your example, that should be something like
Expand|Select|Wrap|Line Numbers
  1. ((Boolean[]) this.boom.get(id))[0].booleanValue()
(Untested, but should work.)

Greetings,
Nepomuk
Oct 13 '10 #4
Oralloy
988 Expert 512MB
Ciary,

The difference between a Vector and an ArrayList is that the Vector class is synchronized. If you're writing a single thread program, you won't notice any difference between the two.

Hopefully that helps.

Luck!
Oct 13 '10 #5
Ciary
247 Expert 100+
hi,

i already solved the problem my own way already. i just made 4 arraylists instead of an arraylist of arrays. that way i can change the type of each arraylist separately.

@Nepomuk: yup, that works. and it's probably better then my method. but since i'm just testing and efficiency isn't an issue i'll only change it if i get stuck.

@Oralloy: i am writing a single thread program so there shouldn't be a difference. but tnx anways. Honestly, i know the name 'vector' from C++ and physics. i had no idea what meaning it had in Java.
Oct 13 '10 #6
Oralloy
988 Expert 512MB
Ciary,

Synchronization requires CPU time. Use it when appropriate, but if it is not needed, then it's waste. But you probably already know that.

What's wrong with using an ArrayList<MyContainer>, where MyContainer is a service class which has all four attributes. Then you access them using a relatively simple construct:
Expand|Select|Wrap|Line Numbers
  1. ArrayList<MyContainer> myArrayList = new ArrayList<MyContainer>();
  2.  
  3. . . .
  4.  
  5. myArrayList.get(index).attributeN;
Luck!
Oct 13 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Graeme Downes | last post by:
I have a string of integer, such as the following: "12 24 12 9 2" and I want to convert this into an array or arraylist which will seperate the string into an array of integers. How do I do...
2
by: Michael Kirchner | last post by:
Hi Ng, I want to create a multiple dimension array in asp. What is the correct way to declare it??? My try was someting like that. but it doesn't work. I'm pleased for some infos... ...
1
by: Craig | last post by:
Hey Everyone - I'm trying to determine the fastest way of moving array information around and could use some help. Here's the setup: Class A stores a DateTime and an amount (a payment)....
2
by: Jarod_24 | last post by:
Does anyone know how efficient the HashList is to lookup values compared to an Array? I know that on large amounts of data, the HashList is better, but i got a list that dosen't seem to get any...
0
by: Simon Gregory | last post by:
I'm trying to override the default elementnames in the (seemingly) simple case of serializing an array of GUIDs into XML. Here's the basic code I started with: Dim arrGuids(3) as Guid ...
2
by: Macca | last post by:
Hi, I need a data structure such as an Array/ArrayList/Generic List to hold multiple instances of a user derfined class. This array will be accessed across multiple threads. However I dont...
2
by: pengbsam | last post by:
Hello All: Having a problem with arraylist.copyto function. And here's a sample of my code: In global--> public struct point { int x; string y; } static public point point;
1
by: pompeez | last post by:
Hi, I need to write a Java Array/ArrayList/List to a text file in Jython. I used the .write() function, but it is letting me write only a string. Thanks! Pompeez
1
by: shwetashah | last post by:
what is the difference between array, arraylist & vector?
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.