在 iPhone 中使用 NSArray 生成随机字符串?

发布于 2024-11-08 12:01:00 字数 653 浏览 0 评论 0原文

我有一个数组,它有一些数据。现在我想随机更改字符串的位置,这意味着想要将字符串洗入数组中。但我不想更改数组的顺序,我只想更改字符串的顺序,而不更改数组索引位置。

我的实际数组是(

         (
        first,
        second,
        third,
        fourth
    ),
        (
        One,
        Two,
        Three,
        Four
    ),
        (
        sample,
        test,
        demo,
        data
    )
)

预期结果,

  (
        (
        second,
        fourth,
        third,
        first
    ),
        (
         Two,
         Four,
         One,
         Three
    ),
        (
        test,
        demo,
        sample,
        data
    )
)

请帮助我。

谢谢!

I have one array and it has some data. Now i want to randomly changed the positions of the strings which means, want to shuffle the strings into the array. But i don't want to change the order of the array, I want to changed only order the strings and doesn't changed the array index position.

My actual array is (

         (
        first,
        second,
        third,
        fourth
    ),
        (
        One,
        Two,
        Three,
        Four
    ),
        (
        sample,
        test,
        demo,
        data
    )
)

Expected result,

  (
        (
        second,
        fourth,
        third,
        first
    ),
        (
         Two,
         Four,
         One,
         Three
    ),
        (
        test,
        demo,
        sample,
        data
    )
)

Please Help me out.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何时共饮酒 2024-11-15 12:01:00

NSIndexSet< /code>就是为此类任务而设计的。

The NSIndexSet is designed for such tasks.

娇纵 2024-11-15 12:01:00

这并不难做到。您应该执行以下操作:

将类别添加到 NSArray。以下实现来自 Kristopher Johnson,正如他在 这个问题

// This category enhances NSMutableArray by providing
// methods to randomly shuffle the elements.
@interface NSMutableArray (Shuffling)
- (void)shuffle;
@end


//  NSMutableArray_Shuffling.m

#import "NSMutableArray_Shuffling.h"

@implementation NSMutableArray (Shuffling)

- (void)shuffle
{


    NSUInteger count = [self count];
    for (NSUInteger i = 0; i < count; ++i) {
        // Select a random element between i and end of array to swap with.
        int nElements = count - i;
        int n = (arc4random() % nElements) + i;
        [self exchangeObjectAtIndex:i withObjectAtIndex:n];
    }
}

@end

现在您有一个名为 shuffle 的方法,它可以对数组进行洗牌。现在,您可以执行以下操作,以便仅对内部数组中的字符串进行打乱:

for (NSMutableArray *array in outerArray) {
   [array shuffle];
}

现在对内部数组进行打乱。
但请记住,内部数组必须是 NSMutableArrays。否则你将无法洗牌它们。 ;-)

桑德罗·迈耶

It's not that hard to do. You should do the following:

Add a category to NSArray. The following implementation is from Kristopher Johnson as he replied in this question.

// This category enhances NSMutableArray by providing
// methods to randomly shuffle the elements.
@interface NSMutableArray (Shuffling)
- (void)shuffle;
@end


//  NSMutableArray_Shuffling.m

#import "NSMutableArray_Shuffling.h"

@implementation NSMutableArray (Shuffling)

- (void)shuffle
{


    NSUInteger count = [self count];
    for (NSUInteger i = 0; i < count; ++i) {
        // Select a random element between i and end of array to swap with.
        int nElements = count - i;
        int n = (arc4random() % nElements) + i;
        [self exchangeObjectAtIndex:i withObjectAtIndex:n];
    }
}

@end

Now you have a method called shuffle, which shuffels your array. Now you can do the following so that only the strings in the inner arrays are shuffled:

for (NSMutableArray *array in outerArray) {
   [array shuffle];
}

Now the inner Arrays are shuffled.
But keep in mind, that the inner arrays need to be NSMutableArrays. Else you won't be able to shuffle them. ;-)

Sandro Meier

陌路终见情 2024-11-15 12:01:00

int 随机索引;

for( int index = 0; index < [array count]; index++ )
{
    randomIndex= rand() % [array count] ;

    [array exchangeObjectAtIndex:index withObjectAtIndex:randomIndex];
}
[array retain];

int randomIndex;

for( int index = 0; index < [array count]; index++ )
{
    randomIndex= rand() % [array count] ;

    [array exchangeObjectAtIndex:index withObjectAtIndex:randomIndex];
}
[array retain];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文