英文:
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<Uri>
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<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();
}
}
}
}
答案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<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));
}
答案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<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); // 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<len; i++) { // Loop for adding them in proportions
if(i<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<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% Array: "+arrayList.get(0));
System.out.println("30% Array: "+arrayList.get(1));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论