java中HashSet怎样遍历取出里面的值?给个例子~~
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java中HashSet怎样遍历取出里面的值?给个例子~~相关的知识,希望对你有一定的参考价值。
参考技术A import java.util.HashSet;import java.util.Iterator;
public class Test
public static void main(String[] args)
HashSet set = new HashSet();
for (int i = 0; i < 20; i++)
set.add(i);
Iterator iterator = set.iterator();
while(iterator.hasNext())
System.out.println((Integer)iterator.next());
参考技术B 用迭代器遍历
Android 中通过Java代码动态生成EditText视图,并循环遍历EditText取出遍历的ID和文本框的值
首先通过java代码实现界面布局,之后点击提交按钮,获取到动态生成EditText视图中用户输入的值。
具体实现如下:
现在xml文件中设置根布局文件id
activity_generate.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GenerateActivity"
android:id="@+id/linear"
android:orientation="vertical"
>
</LinearLayout>
之后在Activity中使用java代码生成布局视图,和获取值的功能
public class GenerateActivity extends AppCompatActivity {
//整个布局的根布局
private LinearLayout linear;
//声明一个EditText类型的list集合
private ArrayList<EditText> list = new ArrayList<>();
private HashMap<Integer, String> hashMap = new HashMap<>();
private static final String TAG = "GenerateActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_generate);
linear = findViewById(R.id.linear);
for (int i = 0; i < 5; i++) {
int controlID = 450 * 100 + i;
String checkName = "数据" + i+":";
hashMap.put(controlID, "CheckID");
CreateTextView(controlID, checkName);
}
CreateButton();
}
private void CreateTextView(int controlID, String checkName) {
//声明一个线性布局管理器
LinearLayout linear1 = new LinearLayout(this);
//设置这个线性布局管理器的宽度和高度
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
linear1.setLayoutParams(params);
//设置线性布局管理器的布局方式 为水平布局
linear1.setGravity(LinearLayout.HORIZONTAL);
TextView textView = new TextView(this);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f);
textView.setLayoutParams(params1);
textView.setText(checkName);
textView.setTextSize(20);
textView.setGravity(CENTER | RIGHT);
linear1.addView(textView);
EditText editText = new EditText(this);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 3.0f);
editText.setLayoutParams(params2);
editText.setId(controlID);
list.add(editText);
linear1.addView(editText);
linear.addView(linear1);
}
private void CreateButton() {
Button button = new Button(this);
button.setText("提交");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//提交数据
SubmitData();
}
});
linear.addView(button);
}
private void SubmitData() {
for (int i = 0; i < list.size(); i++) {
//依次取出输入框中的值
String value = list.get(i).getText().toString();
int id = list.get(i).getId();
String CheckID = hashMap.get(id);
Log.e(TAG, "SubmitData: " + value);
}
}
}
效果图如下:
运行之后就可以获取打印日志:
08-07 16:31:42.996 3236-3236/com.example.daydemo E/GenerateActivity: SubmitData: 12
08-07 16:31:42.996 3236-3236/com.example.daydemo E/GenerateActivity: SubmitData: 13
08-07 16:31:42.996 3236-3236/com.example.daydemo E/GenerateActivity: SubmitData: 14
08-07 16:31:42.996 3236-3236/com.example.daydemo E/GenerateActivity: SubmitData: 15
08-07 16:31:42.996 3236-3236/com.example.daydemo E/GenerateActivity: SubmitData: 16
已经把我们输入的值打印了出来,说明已经获取到了其中的值。
以上是关于java中HashSet怎样遍历取出里面的值?给个例子~~的主要内容,如果未能解决你的问题,请参考以下文章