我想做一个应用程序,它将存储一个数组中的数字,当一个按钮被点击时。数组将从数组中获取一个唯一的随机数,并将其添加到HashSet中。此过程将继续,直到数组为空。并且,当数组为空或HashSet已满时,它会给出一条消息“数组为空,哈希集已满”。我试着自己做,但我做不到。
下面是我的代码:activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="8dp">
<Button
android:id="@+id/array1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="172dp"
android:layout_marginStart="130dp"
android:text="Button" />
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="152dp"
android:text="TextView" />
<TextView
android:id="@+id/textView3"
android:layout_width="276dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignStart="@+id/array1"
android:layout_marginStart="-37dp"
android:layout_marginTop="140dp"
android:text="Range from 0 to 10"
android:textSize="20sp" />
</RelativeLayout>
MainActivity.java
package com.akarki.mathgenius;
import android.content.Intent;
import android.icu.text.UnicodeSetSpanner;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
Random r;
HashSet set;
ArrayList list;
TextView text1;
Button array1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
array1 = (Button) findViewById(R.id.array1);
text1 = (TextView) findViewById(R.id.text1);
set = new HashSet<Integer>();
list = new ArrayList<Integer>(10);
for(int i = 0;i<10;i++){
list.add(i);
}
array1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
arraylist(list,set);
}
});
}
public void arraylist(ArrayList list, HashSet set){
int random = r.nextInt(list.size());
arrayExist((Integer) list.get(random),set);
}
public void arrayExist(int num,HashSet set){
for(int i=0;i<list.size();i++){
if (!set.contains(num)) {
set.add(num);
text1.setText(num);
Toast.makeText(MainActivity.this,"Unique Value",Toast.LENGTH_SHORT).show();
list.remove(num);
}
}
}
}
发布于 2018-09-12 06:01:15
我写了一些代码给你,让你找出问题所在。我发现的一些问题是,首先list.remove(num)
接受一个参数作为index
或Object
,所以你应该将num转换为Integer
。否则,它将抛出ArrayIndexOutOfBoundException
。其次,我不能理解在arrayExist
方法中使用for-loop
的目的。您不需要它,只要您调用onClick
,这个操作就会发生。下面是一段可以在Java中正常运行的测试代码。
@Test
public void fromArrayToSet() {
List<Integer> list = createList();
HashSet<Integer> set = new HashSet<>();
System.out.println("ListBefore = " + list);
System.out.println("SetBefore = " + set);
for(int i = 0; i < 10; i++) {
arraylist((ArrayList) list, set );
}
}
public void arraylist(List<Integer> list, HashSet set){
int random = new Random().nextInt(list.size());
arrayExist(list, list.get(random), set);
}
public void arrayExist(List<Integer> list ,int num, HashSet set){
set.add(num);
list.remove((Integer) num);
System.out.println("ListAfter = " + list);
System.out.println("SetAfter = " + set);
}
public List<Integer> createList() {
List<Integer> list = new ArrayList<Integer>(10);
for(int i = 0;i<10;i++){
list.add(i);
}
return list;
}
https://stackoverflow.com/questions/52284548
复制相似问题