/** * Assigns the specified Object reference to each element of the specified * array of Objects. * * @param a the array to be filled * @param val the value to be stored in all elements of the array * @throws ArrayStoreException if the specified value is not of a * runtime type that can be stored in the specified array */ public static void fill(Object[] a, Object val) { fill(a, 0, a.length, val); } /** * Assigns the specified Object reference to each element of the specified * range of the specified array of Objects. The range to be filled * extends from index <tt>fromIndex</tt>, inclusive, to index * <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the * range to be filled is empty.) * * @param a the array to be filled * @param fromIndex the index of the first element (inclusive) to be * filled with the specified value * @param toIndex the index of the last element (exclusive) to be * filled with the specified value * @param val the value to be stored in all elements of the array * @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt> * @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or * <tt>toIndex > a.length</tt> * @throws ArrayStoreException if the specified value is not of a * runtime type that can be stored in the specified array */ public static void fill(Object[] a, int fromIndex, int toIndex, Object val) { rangeCheck(a.length, fromIndex, toIndex); for (int i=fromIndex; i<toIndex; i++) a[i] = val; }糊了个壳子不用循环?
System.out.println("Hello World");
n--;
if(n>0){
print(n);
}
}
递归就是循环
public void printHelloWorld(int i) {
if (i >= 1) {
printHelloWorld(i - 1);
System.out.println("Hello World");
}
}
System.out.println("Hello World");
System.out.println("Hello World");
System.out.println("Hello World");
System.out.println("Hello World");
……
package com.question;public class Q9 {
public void compute(int i) {
if (i > 0) {
System.out.println("Number " + i);
i --;
compute(i);
}
}
public static void main(String[] args) {
Q9 q9 = new Q9();
q9.compute(9);
}
}
c++允许定义基于statck数据区的Object。由此,不由想到了Java.
在Java的世界里,所有的类型都是引用(或者称为指针), 对象内存的分配都是通过new从heap上显式的分配,无法在Java里构建基于statck数据区的对象。所以在Java里,这道题目是无解的。
Java之于c++,既是进步,又是倒退。
Java语言本身的确帮助c++程序员做了很多事情,比如GC, 去掉了c++中很多复杂的特性,比如多重继承,运算符重载等。
同时,c++本身的很多优点,也丧失了。 写了c++, 然后再写java,一个明显的感觉是,没法使用java写出像c++一样简洁的程序。
比如, 没有了运算符重载, 你不得不使用equal方法来表达两个对象的相等。
Java不能显式表达RAII概念,你不得不使用hard code的方法Log方法的进入和退出, 如:
func() {
log("enter func");
//do something.
log("exit func");
}原文:http://www.cppblog.com/sherrylso/archive/2009/01/13/71831.html
public static void main(String[] args)
{
String org="Hello World";
int n=10;
Object[] strs=new Object[n];
Arrays.fill(strs, org);
String output=Arrays.toString(strs);
output=output.replaceAll("([\\[\\]]|[,][\\s])", "\n");
System.out.println(output);
}
{
String org="Hello World";
int n=10;
Object[] strs=new Object[n];
Arrays.fill(strs, org);
String output=Arrays.toString(strs);
output=output.replaceAll("([\\[\\]]|[,][\\s])", "\n");
System.out.println(output);
}标准答案
int n=10; back1:if(n>0){
System.out.println(org);
}
n--;
if(n>0){
continue back1;
}
我记得java好像也保留了goto关键字
这段代码有两个问题:
1.continue不能在if中使用,至少要在循环中使用。
2.带标签的continue,标签必须定义在希望跳出的最外层循环的前面,这里的back1:至少要放在n--的后面。
* Assigns the specified Object reference to each element of the specified
* array of Objects.
*
* @param a the array to be filled
* @param val the value to be stored in all elements of the array
* @throws ArrayStoreException if the specified value is not of a
* runtime type that can be stored in the specified array
*/
public static void fill(Object[] a, Object val) {
fill(a, 0, a.length, val);
} /**
* Assigns the specified Object reference to each element of the specified
* range of the specified array of Objects. The range to be filled
* extends from index <tt>fromIndex</tt>, inclusive, to index
* <tt>toIndex</tt>, exclusive. (If <tt>fromIndex==toIndex</tt>, the
* range to be filled is empty.)
*
* @param a the array to be filled
* @param fromIndex the index of the first element (inclusive) to be
* filled with the specified value
* @param toIndex the index of the last element (exclusive) to be
* filled with the specified value
* @param val the value to be stored in all elements of the array
* @throws IllegalArgumentException if <tt>fromIndex > toIndex</tt>
* @throws ArrayIndexOutOfBoundsException if <tt>fromIndex < 0</tt> or
* <tt>toIndex > a.length</tt>
* @throws ArrayStoreException if the specified value is not of a
* runtime type that can be stored in the specified array
*/
public static void fill(Object[] a, int fromIndex, int toIndex, Object val) {
rangeCheck(a.length, fromIndex, toIndex);
for (int i=fromIndex; i<toIndex; i++)
a[i] = val;
}糊了个壳子不用循环?
{
String org="Hello World";
int n=10;
Object[] strs=new Object[n];
Arrays.fill(strs, org);
String output=Arrays.toString(strs);
output=output.substring(1,output.length()-1);//去除第一行和最后一行的空行
output=output.replaceAll("([\\[\\]]|[,][\\s])", "\n");
System.out.println(output);
}