473,396 Members | 1,805 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,396 software developers and data experts.

problem with array lists

Dear prob solver,

i have a problem with array lists. i am using array lists for adding objects. i just want to know, when you are adding an object to an arraylist whats the value u pass to the add method???

eg:
i have a class call vertex and i want to add an object from that class. so to add that object how to i add that to the arraylist??? do i need to pass the string id or pass an instant of the relevant class??? The code is below

vertex v;
string source_id

arraylist a = new arraylist();
a.add(source_id??) or a.add(v)??? what is correct?

plz send me an answer asap... cz its for my final year project that i need to submit on next wek.. thnx aloot,,,,,,,,
Ran
Jul 27 '07 #1
5 1479
nateraaaa
663 Expert 512MB
Dear prob solver,

i have a problem with array lists. i am using array lists for adding objects. i just want to know, when you are adding an object to an arraylist whats the value u pass to the add method???

eg:
i have a class call vertex and i want to add an object from that class. so to add that object how to i add that to the arraylist??? do i need to pass the string id or pass an instant of the relevant class??? The code is below

vertex v;
string source_id

arraylist a = new arraylist();
a.add(source_id??) or a.add(v)??? what is correct?

plz send me an answer asap... cz its for my final year project that i need to submit on next wek.. thnx aloot,,,,,,,,
Ran
An array list can hold an array of any type of object that you would like.

for an array of strings you would do something like this.
Expand|Select|Wrap|Line Numbers
  1. string[] arrNames = {"Smith","Jones","Ryan"};
  2. ArrayList a = new ArrayList();
  3. for (int i = 0; i<arrNames.Length; i++)
  4. {
  5. a.Add(arrNames[i].Trim());
  6. }
if you had an array of vertexes try something like this
Expand|Select|Wrap|Line Numbers
  1. object[] arrVertex = {vertex1, vertex2, vertex3};
  2. ArrayList b = new ArrayList();
  3. for(int i = 0; i<arrVertex.Length; i++)
  4. {
  5. b.Add(arrVertex[i]);
  6. }
Give these examples a try and let us know if you encounter any other issues.

Nathan
Jul 27 '07 #2
Plater
7,872 Expert 4TB
If you want to add the vertex object you would add an instance of it.
Expand|Select|Wrap|Line Numbers
  1. vertex v;
  2. string source_id
  3.  
  4. ArrayList a = new ArrayList();
  5. //a.Add(source_id);
  6. //or 
  7. //a.Add(v);<-this one
  8. //??? what is correct? 
  9.  
You should note that any changes made to v after it was added to the ArrayList will not be reflected in the object stored. So make your changes first and do a .Add() after.
Jul 27 '07 #3
Can u plz check the below code and let me know whether its right or wrong???? i have a class called vertex and created an object of that class and those set methods are methods from the vertex class...is this code is correct??? what does that mean actually??? i wanted to know whether the desired operation happens throught this code
thnx aloooot...

ArrayList open_list= new ArrayList();
Vertex ObjvertID = new Vertex();

string[] arrayA= VerticesAdjecnt.ReturnAdjectVertArray(sourceID);
for (int v = 0; v < arrayA.Length; v++)
{
ObjvertID.set_vert_id(arrayA[v]);
ObjvertID.set_parent_node_id(sourceID);
open_list.Add(ObjvertID);

}
Jul 27 '07 #4
--------------------------------------------------------------------------------

Can u plz check the below code and let me know whether its right or wrong???? i have a class called vertex and created an object of that class and those set methods are methods from the vertex class...is this code is correct??? what does that mean actually??? i wanted to know whether the desired operation happens throught this code
thnx aloooot...

ArrayList open_list= new ArrayList();
Vertex ObjvertID = new Vertex();

string[] arrayA= VerticesAdjecnt.ReturnAdjectVertArray(sourceID);
for (int v = 0; v < arrayA.Length; v++)
{
ObjvertID.set_vert_id(arrayA[v]);
ObjvertID.set_parent_node_id(sourceID);
open_list.Add(ObjvertID);

}
















If you want to add the vertex object you would add an instance of it.
Expand|Select|Wrap|Line Numbers
  1. vertex v;
  2. string source_id
  3.  
  4. ArrayList a = new ArrayList();
  5. //a.Add(source_id);
  6. //or 
  7. //a.Add(v);<-this one
  8. //??? what is correct? 
  9.  
You should note that any changes made to v after it was added to the ArrayList will not be reflected in the object stored. So make your changes first and do a .Add() after.
Jul 28 '07 #5
RoninZA
78
ArrayList open_list= new ArrayList();
Vertex ObjvertID = new Vertex();

string[] arrayA= VerticesAdjecnt.ReturnAdjectVertArray(sourceID);
for (int v = 0; v < arrayA.Length; v++)
{
ObjvertID.set_vert_id(arrayA[v]);
ObjvertID.set_parent_node_id(sourceID);
open_list.Add(ObjvertID);

}
This looks like it should work just fine...? Although, you'll have to add a new declaration of your ObjvertID at the start of each loop... otherwise you might be getting funny problems with memory being shared between the objects in the ArrayList. So:
Expand|Select|Wrap|Line Numbers
  1. ArrayList open_list= new ArrayList();
  2. Vertex ObjvertID = new Vertex();
  3.  
  4. string[] arrayA= VerticesAdjecnt.ReturnAdjectVertArray(sourceID);
  5. for (int v = 0; v < arrayA.Length; v++) 
  6. {
  7.     ObjvertID = new Vertex();
  8.     ObjvertID.set_vert_id(arrayA[v]);
  9.     ObjvertID.set_parent_node_id(sourceID);
  10.     open_list.Add(ObjvertID);
  11. }
  12.  
Jul 30 '07 #6

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

Similar topics

8
by: Bo Peng | last post by:
Dear list, I am writing a Python extension module that needs a way to expose pieces of a big C array to python. Currently, I am using NumPy like the following: PyObject* res =...
9
by: Kathryn | last post by:
Hiya I have a problem with using some client side and server side scripting together in an ASP. I'm using VBScript. What I'm trying to achieve is this - - Page loads up and some server side...
21
by: Jeff Thies | last post by:
I'd like to randomly sort an array. A good method?
8
by: rdlebreton | last post by:
Hi, Folks! I've been trying to develop my own version of these draggable layers and I have been limiting myself to IE6...for now. I have looked at some other examples to get ideas of creating...
7
by: Jim Lewis | last post by:
I'm trying to move a function into pyrex for speed. The python side needs to pass a list to the pyrex function. Do I need to convert to array or something so pyrex can generate tight code? I'm not...
5
by: bruce | last post by:
hi... i'm trying to deal with multi-dimension lists/arrays i'd like to define a multi-dimension string list, and then manipulate the list as i need... primarily to add lists/information to the...
5
by: Tobiah | last post by:
I checked out the array module today. It claims that arrays are 'efficient'. I figured that this must mean that they are faster than lists, but this doesn't seem to be the case: ...
36
by: pereges | last post by:
Hi, I am wondering which of the two data structures (link list or array) would be better in my situation. I have to create a list of rays for my ray tracing program. the data structure of ray...
6
by: Jillian Calderon | last post by:
Hi. How do I define a 2d list? For instance, to define a 4 by 5 list, I wanted to do this: n=4 m=5 world = However, it gives me an invalid syntax error saying the index is out of range.
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...

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.