随机按百分比拆分URI的ArrayList,使用Java。

huangapple go评论100阅读模式
英文:

Randomly split arraylist of uri by percentage java

问题

如何按照70%和30%的比例随机分割ArrayList<Uri>

在从设备中选择图像后,我想随机将这个URI列表分成70%和30%两部分。

ArrayList<Uri> imageList = new ArrayList<>();
choose.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
        startActivityForResult(intent, PICK_IMAGE);
    }
});
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_IMAGE) {

        if (resultCode == RESULT_OK) {

            assert data != null;
            if (data.getClipData() != null) {

                int countClipData = data.getClipData().getItemCount();
                int currentImageSelect = 0;

                while (currentImageSelect < countClipData) {

                    imageUri = data.getClipData().getItemAt(currentImageSelect).getUri();

                    imageList.add(imageUri);

                    currentImageSelect = currentImageSelect + 1;

                }

                alert.setText("You have selected " + imageList.size() + " Images");

            } else {
                Toast.makeText(this, "Select multiple images", Toast.LENGTH_SHORT).show();
            }
 
        }
    }

}
英文:

>How to randomly split ArrayList&lt;Uri&gt; into two parts by the percentage of 70% and 30%?

After picking images from the device,i want to split this list of uri into two parts by the percentage of 70 and 30 randomly .

ArrayList&lt;Uri&gt; imageList = new ArrayList&lt;&gt;();
choose.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType(&quot;image/*&quot;);
            intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
            startActivityForResult(intent, PICK_IMAGE);
        }
    });

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_IMAGE) {

        if (resultCode == RESULT_OK) {

            assert data != null;
            if (data.getClipData() != null) {

                int countClipData = data.getClipData().getItemCount();
                int currentImageSelect = 0;

                while (currentImageSelect &lt; countClipData) {

                    imageUri = data.getClipData().getItemAt(currentImageSelect).getUri();

                    imageList.add(imageUri);

                    currentImageSelect = currentImageSelect + 1;

                }

                alert.setText(&quot;You have selected &quot; + imageList.size() + &quot; Images&quot;);

            } else {
                Toast.makeText(this, &quot;Select multiple images&quot;, Toast.LENGTH_SHORT).show();
            }

        }
    }

}

答案1

得分: 1

Collections.shuffle()可以带来随机性。请按以下方式进行操作:

List<Uri> list = new ArrayList<>(imageList);
int pivot = (int) (list.size() * 0.3);

List<Uri> thirtyPercent = new ArrayList<>();
List<Uri> seventyPercent = new ArrayList<>();

Collections.shuffle(list);

for (int i = 0; i < list.size(); i++) {
    if (i <= pivot) thirtyPercent.add(list.get(i));
    else seventyPercent.add(list.get(i));
}
英文:

Collections.shuffle() can bring us the randomness. So do it like the following:

List&lt;Uri&gt; list = new ArrayList&lt;&gt;(imageList);
int pivot = (int) (list.size() * 0.3);

List&lt;Uri&gt; thirtyPercent = new ArrayList&lt;&gt;();
List&lt;Uri&gt; seventyPercent = new ArrayList&lt;&gt;();

Collections.shuffle(list);

for (int i = 0; i &lt; list.size(); i++) {
    if (i &lt;= pivot) thirtyPercent.add(list.get(i));
    else seventyPercent.add(list.get(i));
}

答案2

得分: 1

根据您的问题,您可以首先随机打乱您的ArrayList,然后按照70%和30%的比例划分和选择元素。

以下是执行此操作的函数:

public static ArrayList<ArrayList<String>> shuffleAndDivide(ArrayList<String> arrList) {
    ArrayList<ArrayList<String>> arr = new ArrayList<>();
    ArrayList<String> arr70 = new ArrayList<>();
    ArrayList<String> arr30 = new ArrayList<>();
    Collections.shuffle(arrList); // 随机打乱数组以增加随机性
    int len = arrList.size();
    int fst = (int) Math.floor(0.7 * len); // 计算前70%的元素
    for (int i = 0; i < len; i++) { // 循环按比例添加元素
        if (i < fst) {
            arr70.add(arrList.get(i));
        } else {
            arr30.add(arrList.get(i));
        }
    }
    // 将它们添加到单个ArrayList中
    arr.add(arr70);
    arr.add(arr30);
    return arr;
}

您只需要将String替换为Uri,我在这里只是为了演示目的而写的。
您可以这样使用它:

ArrayList<String> mylist = new ArrayList<>();
mylist.add("Stackoverflow");
mylist.add("Java");
mylist.add("Quiz");
mylist.add("C++");
mylist.add("Practice");
mylist.add("CSharp");
mylist.add("Android");
mylist.add("Php");
mylist.add("Web");
mylist.add("Go");
ArrayList<ArrayList<String>> arrayList = shuffleAndDivide(mylist);
System.out.println("70% 数组: " + arrayList.get(0));
System.out.println("30% 数组: " + arrayList.get(1));

请注意,此函数将ArrayList中的元素按照70%和30%的比例分为两个新的ArrayList,并将它们包含在一个ArrayList中返回。

英文:

According to your question, you can firstly randomly shuffle the ArrayList you have and then divide and pick elements in the proportion of 70% and 30%

Here is the function to do that

public static ArrayList&lt;ArrayList&lt;String&gt;&gt; shuffleAndDivide(ArrayList&lt;String&gt; arrList) {
    ArrayList&lt;ArrayList&lt;String&gt;&gt; arr=new ArrayList&lt;&gt;();
    ArrayList&lt;String&gt; arr70=new ArrayList&lt;&gt;();
    ArrayList&lt;String&gt; arr30=new ArrayList&lt;&gt;();
    Collections.shuffle(arrList);  // Shuffle the array for randomness
    int len=arrList.size();
    int fst=(int) Math.floor(0.7*len); // Calculate the first 70% of elements
    for(int i=0; i&lt;len; i++) { // Loop for adding them in proportions
        if(i&lt;fst) {
            arr70.add(arrList.get(i));
        }else{
            arr30.add(arrList.get(i));
        }
    }
    // Adding them to single ArrayList
    arr.add(arr70);
    arr.add(arr30);
    return arr;
}

You just have to replace String with Uri, I write it here only for demo purpose.
You can use it like that,

ArrayList&lt;String&gt; mylist = new ArrayList&lt;&gt;();
mylist.add(&quot;Stackoverflow&quot;);
mylist.add(&quot;Java&quot;);
mylist.add(&quot;Quiz&quot;);
mylist.add(&quot;C++&quot;);
mylist.add(&quot;Practice&quot;);
mylist.add(&quot;CSharp&quot;);
mylist.add(&quot;Android&quot;);
mylist.add(&quot;Php&quot;);
mylist.add(&quot;Web&quot;);
mylist.add(&quot;Go&quot;);
ArrayList&lt;ArrayList&lt;String&gt;&gt; arrayList=shuffleAndDivide(mylist);
System.out.println(&quot;70% Array: &quot;+arrayList.get(0));
System.out.println(&quot;30% Array: &quot;+arrayList.get(1));

huangapple
  • 本文由 发表于 2020年7月22日 16:28:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63029989.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定