Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 31st, 2008, 5:13 AM   #1
csrocker101
Programmer
 
Join Date: Dec 2006
Posts: 49
Rep Power: 0 csrocker101 is on a distinguished road
Help Indexing ArrayList

Hey everyone I have made an arraylist that takes a stream of information from a string I have made that contains some IP addresses. However I am having a problem assigning the information correctly to my ArrayList. Each IP address is approxiamtely 13 characters long. Here is my code..

string IPAdressesofSystems = service.GetNearbySystems(host);
            ArrayList newList = new ArrayList();

            for (int i = 0; i < IPAdressesofSystems.Length; i++)
            {
                newList.Add(IPAdressesofSystems.Substring(i, 13));
            }

I am well aware it's because of the "i" index I am using in my Substring method is starting at the next incremented index. For example it starts out doing "index[0] = 150.542.234.23 index[1] = 45.543.245.21 index[2] = 4.342.233.29..etc.." because the i is incrementing by one which is making the starting index go up by one everytime. How would I make it so it records all 13 characters of Each IP adress?
csrocker101 is offline   Reply With Quote
Old Mar 31st, 2008, 6:33 AM   #2
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 272
Rep Power: 2 Jabo is on a distinguished road
Re: Help Indexing ArrayList

why would you not split your string into the array? I don't know how getnearbysystems populates the string, but it should separate the ip addresses somehow, and you can use that delimiter to split your string into usable array entries

Quote:
Originally Posted by csrocker101 View Post
string IPAdressesofSystems = service.GetNearbySystems(host);
            ArrayList newList = new ArrayList();

            for (int i = 0; i < IPAdressesofSystems.Length; i++)
            {
                newList.Add(IPAdressesofSystems.Substring(i, 13));
            }

I am well aware it's because of the "i" index I am using in my Substring method is starting at the next incremented index.
What this code does is what you said. If you use the string.split method to separate your string, it will look for whatever delimiter you specify.

I'm not familiar with C#, but in VB it would look something like this:
vb Syntax (Toggle Plain Text)
  1. Dim str1 As String = "xyz,pbb7,rx78"
  2. Dim str2() As String
  3. str2 = str1.Split(",")
  4. For x As Integer = 0 To str2.Length - 1
  5. MsgBox(str2(x), MsgBoxStyle.Information, "Array")
  6. Next

This way, it don't matter what the size of each individual string is. The code I posted displays each array element in a new message box.

Last edited by Jabo; Mar 31st, 2008 at 6:59 AM.
Jabo is offline   Reply With Quote
Old Mar 31st, 2008, 7:25 AM   #3
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 272
Rep Power: 2 Jabo is on a distinguished road
Re: Help Indexing ArrayList

I tried to convert this to C# but I don't know enough about it to really do it.

c# Syntax (Toggle Plain Text)
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. string str1 = "xyz,pbb7,rx78";
  4. string[] str2=new string[]{str1.Split",");
  5. for (int p = 0; p < str2.Length; p++)
  6. {
  7. label1.Text = str2[p]+"\n";
  8. }
  9. }

I get all kinds of errors about my strings and arrays, but maybe it'll help you out at least a little bit.
Jabo is offline   Reply With Quote
Old Mar 31st, 2008, 7:03 PM   #4
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
Re: Help Indexing ArrayList

Use this code with your variables.... remember to replace the comma in the split call with your delimiter.

C# Syntax (Toggle Plain Text)
  1. string str1 = "xyz,pbb7,rx78";
  2. string[] str2 = str1.Split(',');
  3. for (int i = 0; i < str2.Length; i++)
  4. {
  5. textbox1.Text = str2[i]+"\n";
  6. }
__________________
"When in Rome, Do as the Romans Do"
"Beauty is in the eye of the BEER holder"
"Save your breath your going to need it for your blow up doll later"

SearchLores.org
Kilo is offline   Reply With Quote
Old Apr 2nd, 2008, 11:04 AM   #5
Arla
Hobbyist Programmer
 
Arla's Avatar
 
Join Date: Mar 2005
Posts: 227
Rep Power: 4 Arla is on a distinguished road
Re: Help Indexing ArrayList

Or just use i=i+13 in your loop?

If you KNOW each IP is 13 characters, and you don't have a delimiter...
Arla is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
ArrayList Start Index kruptof C# 8 Sep 2nd, 2007 10:20 AM
Problem with an ArrayList Twilight C# 2 Apr 25th, 2006 1:15 PM
ArrayList and MSDN Writlaus C++ 13 Apr 5th, 2006 3:27 AM
ArrayList Troubles crawforddavid2006 Java 17 Jan 10th, 2006 6:37 PM
Converting object to arraylist yusufozkay C# 1 Jul 18th, 2005 8:08 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:02 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC