locked
spliting an array in vb.net RRS feed

  • Question

  • User-1762658977 posted

    hi,

    i am having an array of int like this [1,2,3,4,4,3,2,1] now how to split this single array in to two on both directions

    like this [1,2,3,4] and [4,3,2,1] any help will be greatly appriciated.

    Wednesday, January 4, 2012 2:25 PM

Answers

  • User617492218 posted

    the above code will work if you want your first "split" array to contain 4 indexes, and the rest will go into the second "split" array

    int[] someArray = { 10, 5, 6, 100, 699, 1000, 500, 400, 1 };
    
    int[] splitOne;
    int[] splitTwo;
    
    for (int i = 0; i < someArray.Length; i++)
    {
         if (i < 4)
         {
               splitOne[i] = someArray[i];
         }
         else { splitTwo[i] = someArray[i];
    }
    
    Array.Sort(splitOne);
    Array.Reverse(splitTwo);
    
    foreach (int x in splitOne)
    {
          Console.WriteLine(x);
          Console.WriteLine();
          // output is
          5
          6
          10
          100
    }
    
    foreach (int x in splitTwo)
    {
          Console.WriteLine(x);
          Console.WriteLine();
          // output is
          1000
          699
          500
          400
          1
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 5, 2012 11:25 AM

All replies

  • User-728859679 posted

    so you want to split an array of 8 objects to two arrays of 4 each ?

    or slit any array in half (must have even number of objects)

    Wednesday, January 4, 2012 2:30 PM
  • User617492218 posted

    you can convert to VB using the link below

    this only works if your array is 8 chars

    if you need to sort your array, you can do

    int[] sortedArray = splitOne.Sort();  // ascending  // or Array.Sort(splitOne);
    
    Array.Reverse(splitTwo);  // descending
    int[] someArray = {1, 2, 3, 4, 4, 3, 2, 1};
    
    int[] splitOne;
    int[] splitTwo;
    
    for (int i = 0; i < someArray.Length; i++)
    {
         if (i < 4)
         {
               splitOne[i] = someArray[i];
         }
         else { splitTwo[i] = someArray[i];
    }
    Wednesday, January 4, 2012 2:38 PM
  • User-1762658977 posted

    no actually my array will contain elements both in ascending and descending order with one end point which will be a maximum value.

    like i said [1,2,3,4,4,3,2,1] here the sequence starts with 1 and ends with 4 as ascending then it starts with 4 and ends with 1 as descending.

    where 4 will be the max and the mid point of the array. the count of the array is variable it can also be like this [10,20,30,40,...1000,1000,990,980,970,...10] , i think now i am clear

    Thursday, January 5, 2012 10:43 AM
  • User617492218 posted

    the above code will work if you want your first "split" array to contain 4 indexes, and the rest will go into the second "split" array

    int[] someArray = { 10, 5, 6, 100, 699, 1000, 500, 400, 1 };
    
    int[] splitOne;
    int[] splitTwo;
    
    for (int i = 0; i < someArray.Length; i++)
    {
         if (i < 4)
         {
               splitOne[i] = someArray[i];
         }
         else { splitTwo[i] = someArray[i];
    }
    
    Array.Sort(splitOne);
    Array.Reverse(splitTwo);
    
    foreach (int x in splitOne)
    {
          Console.WriteLine(x);
          Console.WriteLine();
          // output is
          5
          6
          10
          100
    }
    
    foreach (int x in splitTwo)
    {
          Console.WriteLine(x);
          Console.WriteLine();
          // output is
          1000
          699
          500
          400
          1
    }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 5, 2012 11:25 AM
  • User-2065084747 posted

    will there always be exactly the 2 highest (and equal) numbers in the middle,

    the 1st had 4,4

    then 1000,1000

    right ?

    Thursday, January 5, 2012 3:39 PM
  • User617492218 posted

    doesn't matter the order

    it will take the first 4 and split that into the first array, then take the rest (4) and put them into the 2nd

    why don't you plug that code in and i bet  you it will work =)

    Thursday, January 5, 2012 4:27 PM
  • User-728859679 posted

    where 4 will be the max and the mid point of the array. the count of the array is variable it can also be like this [10,20,30,40,...1000,1000,990,980,970,...10] , i think now i am clear

    that is a bit unclear,

    please post multiple example of what the array might look like

    Thursday, January 5, 2012 8:34 PM
  • User-1629487188 posted

    what you are trying to do is not 100% clear,

    will the arrays always have 2 middle maximum values ?

    a simple for loop should do all what you are looking for

    http://msdn.microsoft.com/en-us/library/ch45axte.aspx

    Friday, January 6, 2012 10:27 AM
  • User795332263 posted

    Assuming that your array has a even length and every number in the array have occured twice then you can do this...

    Dim input As Integer() = {1, 2, 3, 4, 4 ,3 ,2 ,1}
    Dim firstarray As Integer() = input.Distinct().ToArray()
    Dim secondarray As Integer() = firstarray.Reverse()

    Thanks

    Arun

    Monday, January 9, 2012 3:04 PM