该代码的真正含义是什么? [英] What does this code really mean?
问题描述
我做了一个用于int的静态方法数组搜索器,目的是为了澄清自己的算法.我做了静态方法:
公共类ArraySearcher公共静态整数integerSearcher(int [] arr,int val){整数匹配= -1;for(int i = 0; i< arr.length; i ++){if(arr [i] == val){匹配=我;休息;}}返回比赛;}}
在main方法中,我创建了一个int数组,并使用我的静态方法在新的int数组中查找已定义的int
int [] anIntArray = {20,30,40,60};int searchingAnArray = ArraySearcher.integerSearcher(anIntArray,60);如果(searchingAnArray == -1){System.out.println(找不到匹配项");} 别的 {System.out.println(找到的结果:" + anIntArray [searchingAnArray]);}
问题是我不理解 System.out.println(结果发现:" + anIntArray [searchingAnArray]);"
,这实际上意味着什么.
System.out.println(结果已找到:" + anIntArray [searchingAnArray]);
将在 searchingAnArray
位置上查找 anIntArray
.IE.将在位置 3
(3来自您的方法)中查找数组 anIntArray
的值,然后将打印:
结果:60
如果您是说 另一个例子: 它将打印 不,只是开个玩笑,它会显示: 另一个例子: 将打印: 如您所见,它将连接一个 它可以看作是: 如果您在 希望这就是您要寻找的,尽我所能最好地解释. I made a static method array searcher for int for some pratise to clear myself for making algorithm. I made the static method: And in the main method I created an array of int and used my static method for finding a defined int in the new int array The question is that I did'n't understand the Will look for If you mean what does Another example: It will print nah, just joking, it will print: Another example: And will print: As you can see it will concatenate a It could be seen as: If you call concatenation inside Hopefully that is what you're looking for, the best explained as I could. 这篇关于该代码的真正含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! +
是什么意思,就像@HotLicks在他的 >
字符串名称="Chuck Norris";System.out.println("Hello" +名称);
你好,你不是Chuck Norris
Hello Chuck Norris
int年= 2014,天= 12,月= 8;//这可以用日期类型完成,但出于演示目的,我将这样做.System.out.println("Today is:" + day +"/" + month +"/" + year);
今天是:2014/12/8
String
和一个 int
,依此类推,然后进行打印.
字符串stringToBePrinted =今天是:" +天+"/" +月+"/" +年;System.out.println(stringToBePrinted);
System.out.println(...)
内部调用串联,它将执行上述操作(以隐式方式),例如执行创建局部变量",然后打印它.(不完全是这样,但是您可以像这样理解它).public class ArraySearcher
public static int integerSearcher(int[] arr,int val){
int match = -1;
for(int i = 0; i < arr.length; i++){
if(arr[i]==val){
match = i;
break;
}
}
return match;
}
}
int[] anIntArray = { 20, 30, 40, 60 };
int searchingAnArray = ArraySearcher.integerSearcher(anIntArray, 60);
if (searchingAnArray == -1){
System.out.println("match not found");
} else {
System.out.println("result found: "+ anIntArray[searchingAnArray]);
}
System.out.println("result found: "+ anIntArray[searchingAnArray]);"
and what does this really means.System.out.println("result found: "+ anIntArray[searchingAnArray]);
anIntArray
on searchingAnArray
position. I.e. will look for the value of the array anIntArray
at position 3
(3 comes from your method), then it will print:result found: 60
+
means, is, as @HotLicks said on his comment, a concatenation. It will convert 60
into a String
and add it to the same String
as "result found: "
String name = "Chuck Norris";
System.out.println("Hello " + name);
Hello you're not Chuck Norris
Hello Chuck Norris
int year = 2014, day = 12, month = 8; //This could be done with a date type but for demonstration purposes I'll do it like this.
System.out.println("Today is: " + day + "/" + month + "/" + year);
Today is: 12/8/2014
String
and then an int
and so on, then print it.String stringToBePrinted = "Today is: " + day + "/" + month + "/" + year;
System.out.println(stringToBePrinted);
System.out.println(...)
it will do something like the above (in an implicit way), something like "creating a local variable" and then print it. (Not exactly that way but you can see it like that to understand it).