Java 方法,使用for循环将数字添加到arraylist

Java 方法,使用for循环将数字添加到arraylist,java,for-loop,arraylist,methods,Java,For Loop,Arraylist,Methods,此方法将用数字1到10填充ArrayList,同时使用不带参数的for循环。我尝试的方法是在顶部附近创建钉板。为了实现这一点,我应该为for循环填写什么 import java.util.*; import java.util.Scanner; public class PegBoardGame { public static ArrayList create_pegboard(){ //for loop and add method to holes 1-10

此方法将用数字1到10填充
ArrayList
,同时使用不带参数的for循环。我尝试的方法是在顶部附近创建钉板。为了实现这一点,我应该为for循环填写什么

import java.util.*;
import java.util.Scanner;

public class PegBoardGame {

    public static ArrayList create_pegboard(){
        //for loop and add method to holes 1-10
        for(){

        }
    }

    public static void print_pegboard(ArrayList pegboard) {
        //print results from array 1-10 or final result
        System.out.println("-----------------------------------------");
        System.out.println(pegboard);
        System.out.println("-----------------------------------------");
    }

    public static Integer peg_hole(ArrayList pegboard){
        Scanner in = new Scanner(System.in);
        //variable for user input
        int holetofillInt;
        int checkInt;
        //prompt for input
        System.out.println("Select a peghole 1-10 to fill");
        holetofillInt = in.nextInt();
        //if peg chosen by user is already 0 then print error message
        checkInt = pegboard[holetofillInt];
        if(  ){ 
            System.out.println("Peghole is already filled!");
        }
        else{
            //set selected hole to 0
            pegboard.set(holetofillInt,0);
            return holetofillInt;
        }
    }


    public static void main(String[] args) {
        //create array list with peghole numbers
        ArrayList<Integer> pegboard = create_pegboard();
        //print the pegboard unchanged
        print_pegboard(pegboard);
        //construct array list up to 10
        for (int i = 0; i < 10; i++) {
            //see if they want to change and change what hole is peggged
             peg_hole(pegboard);
             //print changed peghole board
             print_pegboard(pegboard);
        }
    }
}
import java.util.*;
导入java.util.Scanner;
公共类跳板游戏{
publicstaticarraylistcreate_pegboard(){
//用于循环并将方法添加到孔1-10
for(){
}
}
公共静态空白打印钉板(ArrayList钉板){
//打印数组1-10的结果或最终结果
System.out.println(“--------------------------------------------------------”;
系统输出打印LN(钉板);
System.out.println(“--------------------------------------------------------”;
}
公共静态整型钉孔(ArrayList钉板){
扫描仪输入=新扫描仪(系统输入);
//用于用户输入的变量
int holetofillInt;
int-checkInt;
//提示输入
System.out.println(“选择要填充的桩孔1-10”);
holetofillInt=in.nextInt();
//如果用户选择的peg已为0,则打印错误消息
checkInt=钉板[holetofillInt];
如果(){
System.out.println(“PEGHORE已填充!”);
}
否则{
//将选定孔设置为0
钉板组(holetofillInt,0);
返回孔填充;
}
}
公共静态void main(字符串[]args){
//使用桩孔编号创建阵列列表
ArrayList pegboard=创建_pegboard();
//原封不动地打印钉板
印刷钉板(钉板);
//最多可构造10个数组列表
对于(int i=0;i<10;i++){
//看看他们是否想改变,改变什么洞被钉住
钉孔(钉板);
//打印更改的钉孔板
印刷钉板(钉板);
}
}
}

如果没有传入任何参数,请确保数组在整个类中都有作用域,然后以与方法外部相同的方式执行


我的建议是将数组传入。

您的方法应该创建一个新的整数数组列表,然后使用for循环用数字1-10填充它,然后返回您创建的新填充列表

这正是你想要的

public static ArrayList create_pegboard(){
//for loop and add method to holes 1-10
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i = 1; i<=10; i++){
    list.add(i);
     }
return list;
}
publicstaticarraylistcreate_pegboard(){
//用于循环并将方法添加到孔1-10
ArrayList=新建ArrayList();

for(int i=1;i你有什么问题?我不知道在不使用参数的情况下从何处开始for循环如果你有参数,你会从何处开始for循环?