7 Replies - 452 Views - Last Post: 11 April 2010 - 12:43 PM Rate Topic: -----

#1 RckStr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 07-April 10

Array help

Posted 11 April 2010 - 10:03 AM

Is there a way to clear every other index that is stored in an array? I was looking at Array.Clear but I don't need it to clear from one index to another. I need something like:


[0][1x][2][3x][4][5x]

and


[0x][1][2x][3][4x][5]

*x is one to be cleared

This post has been edited by RckStr: 11 April 2010 - 10:04 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Array help

#2 gda2004  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 111
  • Joined: 08-January 09

Re: Array help

Posted 11 April 2010 - 10:27 AM

View PostRckStr, on 11 April 2010 - 09:03 AM, said:

Is there a way to clear every other index that is stored in an array? I was looking at Array.Clear but I don't need it to clear from one index to another. I need something like:


[0][1x][2][3x][4][5x]

and


[0x][1][2x][3][4x][5]

*x is one to be cleared

A for loop would probably be best in this case where you cycle through each one and set it to "" or every other one in this case you could use a double increment to acheive this

This post has been edited by gda2004: 11 April 2010 - 10:28 AM

Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1619
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Array help

Posted 11 April 2010 - 10:28 AM

You cannot remove things from a string array like that (or an integer array). If you need this functionality I would look into using an ArrayList or some other form of collection
Was This Post Helpful? 0
  • +
  • -

#4 gda2004  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 111
  • Joined: 08-January 09

Re: Array help

Posted 11 April 2010 - 10:36 AM

View PostPsychoCoder, on 11 April 2010 - 09:28 AM, said:

You cannot remove things from a string array like that (or an integer array). If you need this functionality I would look into using an ArrayList or some other form of collection

why can't you clear data from an array using a for loop? I know its not actually removing the array as such but it would clear the data from it
Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1619
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Array help

Posted 11 April 2010 - 10:38 AM

Because it's just no possible in the sense the OP is looking for. Sure you can set it to an empty string but the index will still remain. You cannot create a string array of size 4, remove 2 items and have it be a size of 2. It will remain with 4 indexes no matter what
Was This Post Helpful? 0
  • +
  • -

#6 gda2004  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 111
  • Joined: 08-January 09

Re: Array help

Posted 11 April 2010 - 10:40 AM

View PostPsychoCoder, on 11 April 2010 - 09:38 AM, said:

Because it's just no possible in the sense the OP is looking for. Sure you can set it to an empty string but the index will still remain. You cannot create a string array of size 4, remove 2 items and have it be a size of 2. It will remain with 4 indexes no matter what

I understand that but is he asking to delete the index or simply clear the data at the index ? and if he want to delete the index then Vectors are probably better

Regards,
Gda2004

This post has been edited by gda2004: 11 April 2010 - 10:42 AM

Was This Post Helpful? 0
  • +
  • -

#7 RckStr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 07-April 10

Re: Array help

Posted 11 April 2010 - 11:40 AM

Bummer, How about a way to load every other index into its own array? Do you run into the same problems? Say you have a populated array of strings that are in first and last name order, can you then separate the first and last names into their own array? like:

[0F][1L][2F][3L][4F][5L]

into parallel arrays.

[0F][1F][2F]

and

[0L][1L][2L]
Was This Post Helpful? 0
  • +
  • -

#8 Ethryx  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 9
  • Joined: 08-April 10

Re: Array help

Posted 11 April 2010 - 12:43 PM

This should do what you requested in your last reply:

Sub Main()
        Dim NamesOfPeople() As String = {"Daniel", "Radcliffe", "Emma", "Watson", "Rupert", "Grint"}
        Dim half As Integer = (NamesOfPeople.Length / 2)
        Dim FirstNames(half-1) As String
        Dim LastNames(half-1) As String

        For x As Integer = 1 To half
            FirstNames(x - 1) = NamesOfPeople(x + (x - 2)) '0 2 4
            LastNames(x - 1) = NamesOfPeople(x + (x - 1)) '1 3 5
        Next

        'Show result
        For x As Integer = 1 To half
            Console.WriteLine("First: " & FirstNames(x - 1) & ", Last: " & LastNames(x - 1))
        Next
    End Sub



It uses an array that contains 3 names (6 entries). Then, it creates two new arrays that are half the size for first names and last names. This half value is subtracted by 1 because of the zero-based array's. The first loop will populate the two new arrays, and the second loop will output the result to make sure it worked correctly.

This post has been edited by Ethryx: 11 April 2010 - 12:45 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1