这个公式在java中是合法的来声明ArrayList
List<Integer> list1 = new ArrayList<Integer>() {1,2,3,4,5};
Run Code Online (Sandbox Code Playgroud) 我想以小批量大小迭代ArrayList.
例如,如果ArrayList大小为75且批量大小为10,我希望它处理0-10,然后是10-20,然后是20-30等记录.
我试过这个,但它不起作用:
int batchSize = 10;
int start = 0;
int end = batchSize;
for(int counter = start ; counter < end ; counter ++)
{
if (start > list.size())
{
System.out.println("breaking");
break;
}
System.out.println("counter " + counter);
start = start + batchSize;
end = end + batchSize;
}
Run Code Online (Sandbox Code Playgroud) 我有一个ArrayList重复的数字:
1
2
3
1
2
4
Run Code Online (Sandbox Code Playgroud)
我想要做的是打印程序1,2,3,4并忽略已经打印的整数.
我通常采用的方法是使用for循环遍历ArrayList,但我正在努力阻止元素重复.我在想某种计数器系统可能在这里工作,但我不确定实现它,所以任何帮助将不胜感激.
我想用两个条件打印两个arraylists的元素.但是在尝试实现它时我得到了Concurrent Modification异常.
我做错了什么?
public void Insert_Answers(Enumeration<String> ques, Enumeration<String> ans){
try {
while(ans.hasMoreElements()){
String answer = ans.nextElement();
System.out.println(answer);
while(ques.hasMoreElements()){
String question = ques.nextElement();
System.out.println(question);
}
}
}
catch (Exception ex) {
Logger.getLogger(DbInsert.class.getName()).log(Level.SEVERE, null, ex);
}
}
Run Code Online (Sandbox Code Playgroud)
这是包含main方法的Test.java类
public class Test {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
DbInsert dbIns = new DbInsert();
ArrayList<String> arr1 = new ArrayList<String>();
arr1.add("a");
arr1.add("b");
arr1.add("c");
arr1.add("d");
Enumeration earr1 = Collections.enumeration(arr1);
ArrayList<String> arr2 = new ArrayList<String>();
arr2.add("e");
arr1.add("f"); …Run Code Online (Sandbox Code Playgroud) 我有一个CustomClass,Bean/Model/Pojo它用于从Json保存自定义Obejcts.我希望保存ArrayList我CustomClass的内容SharedPreference并在特殊情况下将其恢复.我发现它存储在一个方式SharedPreference 在这里!正确的答案是SpyZip
我能够存储和检索一个,List但我想存储和检索一个ArrayList.
这是一个片段,显示我如何存储和检索值SharedPreference:
// This method will save custom class ArrayList<Bean>
public void saveUserFavouriteStations(
ArrayList<RadioStationBean> radioStation) {
FavouriteStationHolder = this.getSharedPreferences("stations", 0);
Editor editor = FavouriteStationHolder.edit();
Gson gson = new Gson();
String jsonCars = gson.toJson(radioStation);
editor.putString("stations", jsonCars);
System.out.println("Custom ArrayList Saved in App Class");
editor.commit();
}
public ArrayList<RadioStationBean> getUserFavouriteStations() {
FavouriteStationHolder = this.getSharedPreferences("stations", 0);
if (FavouriteStationHolder != null) {
String jsonString = …Run Code Online (Sandbox Code Playgroud) 添加方法给出了错误ArrayList.它没有添加.
public List<String> arrayList;
...
arrayList = new ArrayList<String>();
...
savenumberButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String str = numberText.getText().toString();
Integer cout = listView.getCount()+ 1;
String str1 = cout.toString().concat("."+str);
try {
arrayList.add(listView.getCount(), str1);
} catch (Exception e) {
Log.w("Error", "arraylist.add is not running!");
}
arrayAdapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "Data Saved", Toast.LENGTH_SHORT).show();
blockNumberText.setText(" ");
}
});
...
Run Code Online (Sandbox Code Playgroud) 我有一个arrayList的问题.这是我的arrayList代码
我知道我的arrayList方法有一些问题,但我无法发现它.
arrayList的方法
public ArrayList<Tour> countryList(String country)
{
ArrayList<Tour> newList = null;
switch (country)
{
case "Indonesia":
country = "IN";
break;
case "Thailand":
country = "TH";
break;
case "Singapore":
country = "SI";
break;
case "Brunei":
country = "BR";
break;
case "Philipines":
country = "PH";
break;
case "Vietnam":
country = "VI";
break;
case "Laos":
country = "LA";
break;
case "Myammmar":
country = "MY";
break;
case "Malaysia":
country = "MS";
break;
}
if(TList.indexOf(country)!= 1)
{
return newList;
}
return newList;
}
Run Code Online (Sandbox Code Playgroud)
在主要班级 …
我试图walls ArrayList用for循环删除里面的所有对象,但我不能让它工作
public void initializeButtons(){
JButton save_button = new JButton("Save");
save_button.addMouseListener(new Button_MouseListener(save_button));
save_button.setBounds(1300, 50, 75, 50);
add(save_button);
JButton load_button = new JButton("Load");
save_button.addMouseListener(new Button_MouseListener(load_button));
load_button.setBounds(1300, 125, 75, 50);
add(load_button);
JButton reset_button = new JButton("Reset");
save_button.addMouseListener(new Button_MouseListener(reset_button));
reset_button.setBounds(1300, 200, 75, 50);
add(reset_button);
}
Run Code Online (Sandbox Code Playgroud)
这是Button_MouseListener负责处理"Reset"按钮的一部分
if (button.getText().equals("Reset")) {
for (int i = 0; i < walls.size(); i++) {
walls.remove(((int)i));
}
repaint();
}
Run Code Online (Sandbox Code Playgroud)
注意:
walls.clear();也不起作用,我没有收到任何错误.它什么都不做.
我想创建一个由treeNodes组成的数组Arraylist.我的审判是
ArrayList<Arrays<treeNode>> aList = new ArrayList<Arrays<treeNode>>();
Arrays<TreeNode> aNodes = new ArrayList<TreeNode>();
Run Code Online (Sandbox Code Playgroud)
但它给出了一个错误.(包括utils)
写这个的正确方法是什么?我的目标是在二叉树中找到节点的最小深度(只是为了找到最小的不找到该节点本身,我将水平放入arraylist,一旦大小不是2 ^ j,那么最小级别是j-1 ).
提前感谢,任何帮助/提示/解决方案......
我想在列表中存储元素,每个元素有4个参数我正在尝试创建一个数组列表,为每个元素存储4个不同类型的参数:
iD:int
x position:float
y position:float
name:string
我用:
ArrayList<String> activList ;
Run Code Online (Sandbox Code Playgroud)
但是当我使用时:
activList.add(2, 4.5, 8.9,"Name");
Run Code Online (Sandbox Code Playgroud)
我收到错误:
"ArrayList类型中的方法add(int,Object)不适用于参数(in,float,float)"
我不知道如何能够为ArrayList添加不同的类型,有没有办法做到这一点?
谢谢你的帮助
arraylist ×10
java ×9
android ×2
add ×1
arrays ×1
binary-tree ×1
collections ×1
duplicates ×1
for-loop ×1
list ×1
logcat ×1
methods ×1
processing ×1
tree ×1