Java 将变量分配给';对于';环
我只是想知道是否可以给整个循环分配一个变量,因为我将多次使用同一个变量。我是一个相当的新手。。。不要对我太苛刻Java 将变量分配给';对于';环,java,variables,for-loop,Java,Variables,For Loop,我只是想知道是否可以给整个循环分配一个变量,因为我将多次使用同一个变量。我是一个相当的新手。。。不要对我太苛刻 for (m = 0 ; m<=Student2.size()-1; m++) { System.out.println(Student2.get(m)); } for(m=0;m我相信你想要的技术术语是: 旁注:如果Student2是一个变量名,那么它应该是小写的。我相信你想要的技术术语是: 旁注:如果Student2是一个变量名,那么它应该是小写的。您应该阅读以下内
for (m = 0 ; m<=Student2.size()-1; m++)
{
System.out.println(Student2.get(m));
}
for(m=0;m我相信你想要的技术术语是:
旁注:如果Student2
是一个变量名,那么它应该是小写的。我相信你想要的技术术语是:
旁注:如果Student2
是一个变量名,那么它应该是小写的。您应该阅读以下内容:
您不能将代码放入可以获取参数和返回值的方法/函数中,这些函数是您可以随意调用的代码段。示例:
public static void main(String[] args) throws Exception {
doCalculation(3,5); //call the method with two arguments
doCalculation(7,2); //call the method again with other arguments
}
//define a method in this way: visibilty, return typ, name, arguments
public static int doCalculation(int numb1, int numb2) {
int result = numb1 * numb2;
return result;
}
您的函数应该如下所示(假设列表包含字符串类型的对象):
publicstaticvoidmain(字符串[]args)引发异常{
学生(学生2);
}
公共静态无效打印学生(ArrayList学生列表){
对于(int m=0;m,您应该阅读以下内容:
您不能将代码放入可以获取参数和返回值的方法/函数中,这些函数是您可以随意调用的代码段。示例:
public static void main(String[] args) throws Exception {
doCalculation(3,5); //call the method with two arguments
doCalculation(7,2); //call the method again with other arguments
}
//define a method in this way: visibilty, return typ, name, arguments
public static int doCalculation(int numb1, int numb2) {
int result = numb1 * numb2;
return result;
}
您的函数应该如下所示(假设列表包含字符串类型的对象):
publicstaticvoidmain(字符串[]args)引发异常{
学生(学生2);
}
公共静态无效打印学生(ArrayList学生列表){
对于(int m=0;m我不理解这个问题。您的代码是完全合法的。您是否引用了?输入方法更好:)可能更简单、更容易理解。:)我的意思是,我在代码中多次使用这个确切的循环。只是想知道是否有办法缩短它。就像你不写5+5一样,你可以只指定一个变量:summ=5+5…我不知道我是否正确解释了我不理解这个问题。你的代码是完全合法的。你是指一个?输入方法I好主意:)可以更简单、更容易理解。:)我的意思是,我在代码中多次使用这个确切的循环。只是想知道是否有办法缩短它。就像你不写5+5一样,你可以只指定一个变量:summ=5+5…我不知道我是否正确解释了自己我明白了…但在我的例子中,哪一个是参数?在你的例子中,它的参数Student2是一个对象,但我不知道它的类型。我想你调用了Student2是student类对象的类,对吗?啊,好吧,这个列表包含哪种类型的数据?student对象?还是字符串?我发布了你需要的函数,假设ArrayList包含字符串。它只包含strings我看到了…但是我在我的例子中,哪个是参数?在你的例子中是参数Student2是一个对象,但我不知道类型。我想你调用了Student2是student类对象的类,对吗?啊,好的,这个列表包含哪种类型的数据?student对象?还是字符串?我发布了你需要的函数,假设ArrayList包含字符串。它只包含字符串循环将更好此方式:for(int m=0;m循环将更好此方式:for(int m=0;m
public static void main(String[] args) throws Exception {
doCalculation(3,5); //call the method with two arguments
doCalculation(7,2); //call the method again with other arguments
}
//define a method in this way: visibilty, return typ, name, arguments
public static int doCalculation(int numb1, int numb2) {
int result = numb1 * numb2;
return result;
}
public static void main(String[] args) throws Exception {
printStudents(Student2);
}
public static void printStudents(ArrayList<String> studentList) {
for (int m = 0; m <= studentList.size()-1; m++)
{
System.out.println(studentList.get(m));
}
}