【问题标题】:Array is not shuffling in android数组在android中没有洗牌
【发布时间】:2016-07-29 17:43:40
【问题描述】:

我正在尝试通过将数组转换为 List、调用 shuffle 方法,然后将其转换回同一个数组来对数组进行混洗。但是,由于某种原因,数据没有洗牌。是因为它是静态的吗?我没有错误,它只是不洗牌。谢谢。

public static String IMGS[] = {
            "http://i.imgur.com/F6WgOZg.jpg",
            "https://images.unsplash.com/photo-1439546743462-802cabef8e97?dpr=2&fit=crop&fm=jpg&h=725&q=50&w=1300",
            "https://images.unsplash.com/photo-1441155472722-d17942a2b76a?q=80&fm=jpg&w=1080&fit=max&s=80cb5dbcf01265bb81c5e8380e4f5cc1",
            "https://images.unsplash.com/photo-1437651025703-2858c944e3eb?dpr=2&fit=crop&fm=jpg&h=725&q=50&w=1300",
            "https://images.unsplash.com/photo-1431538510849-b719825bf08b?dpr=2&fit=crop&fm=jpg&h=725&q=50&w=1300",
            "https://images.unsplash.com/photo-1434873740857-1bc5653afda8?dpr=2&fit=crop&fm=jpg&h=725&q=50&w=1300",
            "https://images.unsplash.com/photo-1439396087961-98bc12c21176?dpr=2&fit=crop&fm=jpg&h=725&q=50&w=1300",
            "https://images.unsplash.com/photo-1433616174899-f847df236857?dpr=2&fit=crop&fm=jpg&h=725&q=50&w=1300",
            "https://images.unsplash.com/photo-1438480478735-3234e63615bb?dpr=2&fit=crop&fm=jpg&h=725&q=50&w=1300",
            "https://images.unsplash.com/photo-1438027316524-6078d503224b?dpr=2&fit=crop&fm=jpg&h=725&q=50&w=1300"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        List<String> arr = Arrays.asList(IMGS);
        Collections.shuffle(arr);
        arr.toArray(IMGS);

【问题讨论】:

    标签: android shuffle


    【解决方案1】:

    我想你忘了使用 arr.toArray 的返回值作为对 IMGS 的赋值

        List<String> arr = Arrays.asList(IMGS);
        Collections.shuffle(arr);
        IMGS = arr.toArray(IMGS);
    

    【讨论】:

    • 嗯,它仍然没有洗牌。
    • @Brandex07 它应该在这个改变之后洗牌。我在我的 IDE 中试过了
    • 嗯我会尝试重新启动ide...也许模拟器没有更新
    • OHH 我的意思是更改另一个我称为静态上下文的数组。
    猜你喜欢
    • 2015-04-15
    • 2012-09-01
    • 1970-01-01
    • 2013-06-28
    • 1970-01-01
    • 1970-01-01
    • 2015-12-11
    • 1970-01-01
    相关资源
    最近更新 更多