0

Firstly thank you for taking the time to look at my question.

I have a csv file of letters for which i need to get the last letter of the array and move it to the start while "pushing" the other letters across

E.G.

--Source-- a,b,c,d,[e]

--Rotated-- e,a,b,c,d

            for (var i = 0; i < Array.Length - 1; i++)
            {
                temp = Array[Array.Length];
                Array[Array.Length] = Array[Array.Length - 1];
                Array[i + 1] = Array[i];
                Array[i] = temp;

            }

For this I am aware that not all characters would be effected but i cant think of a loop to get all values moved

2
  • First things first, there is no element at index Array.Length. The last index is 1 less than that. Think of the logic first - write it down if you have to - and then write code to implement that logic. First, put the last element in a temporary location. Then go from the second last element to the first, moving each element to the next index. Finally, move the temp value into the first element. So, you've got code inside the loop that belongs outside, your indexing is wrong and your loop is backwards. Commented May 17, 2018 at 5:38
  • why dont you make use of list , that will resolve easily Commented May 17, 2018 at 5:43

3 Answers 3

2

Use Copy method:

int last = arr[arr.Length - 1];
Array.Copy(arr, 0, arr, 1, arr.Length - 1);
arr[0] = last;
Sign up to request clarification or add additional context in comments.

Comments

1

You can shift the numbers to right by using the modulo % operator :

int[] arr = { 1, 2, 3, 4, 5 };
int[] newArr = new int[arr.Length];
for (int i = 0; i < arr.Length; i++)
{
    newArr[(i + 1) % newArr.Length] = arr[i];
}

newArr = {5,1,2,3,4}

DEMO HERE

EDIT:

Or you could make a method that shifts the numbers in your initial array without the need for creating a new array. The method rightShiftArray takes two parameters, the initial array arr an the number of shifts (shift) you want to perform:

public void rightShiftArray(ref int[] arr, int shift)
{
    for (int i = 0; i < shift; i++)
    {
        int temp;
        for (int j = 0; j < arr.Length - 1; j++)
        {
            temp = arr[j];
            arr[j] = arr[arr.Length - 1];
            arr[arr.Length - 1] = temp;
        }
    }
}

For example:

int[] arr = { 1, 2, 3, 4, 5 };

rightShiftArray(ref arr, 2);

The code above shifts the numbers in the initial array arr twice to the right and gives you the following output:

arr = { 4, 5, 1, 2, 3};

DEMO HERE

Comments

0

if you doesn't want to allocate new array, you can use this code :

newValue = Array[Array.Length-1];
for (var i = 0; i < Array.Length; i++)
{
  temp = Array[i];
  Array[i] = newValue;
  newValue = temp;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.