2

I'm sure there's a word for what I,m looking for but since I don't know it I can't find the answear to my problem, what I have is a jagged array of double and I want the value at index 0 to be the value of index 1 same for the index 1 going to the 2 until the end of the array and the last index being pish to the index 0

Example :

Original

private double[][] array = { {1, 2, 3, 4}, {5, 6, 7, 8}, ...}

Become (After the modification) I know it's seems I'm redeclaring the array that's not the point, it's just to show you what it should be after.

private double[][] array = { {4, 1, 2, 3}, {8, 5, 6, 7}, ...}

EDIT

If you know the word or somethings about what I'm looking for, could you say so in the comment I will delete the question and look further into it

3
  • A missclick I guess, sorry
    – Mokmeuh
    Feb 8, 2014 at 16:22
  • That is usually referred to as rotation. I don't believe that capability is built into arrays. How about using queues instead of arrays? Feb 8, 2014 at 16:22
  • Never used queues, but if I do so I need to be able to fill them with linq, is it possible ?
    – Mokmeuh
    Feb 8, 2014 at 16:24

2 Answers 2

3

This is usually called rotating. You can achieve it a for-loop, but note that since your rotating to the right, it's easier to work your way from back to front, like this:

for(var i = 0; i < array.Length; i++)
{
    var len = array[i].Length;
    if (len > 1) {
        var last = array[i][len - 1];
        for(var j = len - 1; j > 0; j--)
        {
            array[i][j] = array[i][j - 1];
        }
        array[i][0] = last;
    }
}
2
  • would it be possible to go right ???, I guess so, I will just do the for loop manually to get it
    – Mokmeuh
    Feb 8, 2014 at 16:35
  • @Mokmeuh Yes, you can rotate in either direction. You just have to reverse the direction of the inner loop.
    – p.s.w.g
    Feb 8, 2014 at 16:38
1

You could easily use a List of Queue instead of a jagged array. Below an example of how your jagged array could look like and how to move elements:

var queues = new List<Queue<double>>();

//first queue (1,2,3,4)
var queue = new Queue<double>();
queue.Enqueue(4);
queue.Enqueue(3);
queue.Enqueue(2);
queue.Enqueue(1);
queues.Add(queue);

//second queue (5,6,7,8)
var queue2 = new Queue<double>();
queue.Enqueue(8);
queue.Enqueue(7);
queue.Enqueue(6);
queue.Enqueue(5);
queues.Add(queue2);

//an example of how to "rotate"
var lastItem = queues[0].Dequeue(); // (1,2,3)
queues[0].Enqueue(lastItem); // (4,1,2,3)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.