
关于Java字符串数组长度的问题,新手求教
String[]qwe=newString[]{"ihopetomorrowwouldbebetter!"};System.out.println(qwe.length)...
String[] qwe= new String []{"i hope tomorrow would be better!"};
System.out.println(qwe.length);
输出显示输出的值为1啊 ,为什么?
其实我想将这段话倒着输出的
for (int i=0 ; i<qwe.length ; i++)
{
String qqq=qwe[i];
System.out.print(qqq);
}
结果仍然是原来的顺序输出。。。
我最想问的是上面的长度值为什么为1。。。? 展开
System.out.println(qwe.length);
输出显示输出的值为1啊 ,为什么?
其实我想将这段话倒着输出的
for (int i=0 ; i<qwe.length ; i++)
{
String qqq=qwe[i];
System.out.print(qqq);
}
结果仍然是原来的顺序输出。。。
我最想问的是上面的长度值为什么为1。。。? 展开
展开全部
String[] qwe= new String []{"i hope tomorrow would be better!"};
数组里只有一个元素,是 i hope tomorrow would be better!
String数组中放的是字符串,你这个只放了一个字符串,所以长度是1
String[] str = new String[]{"hello", "world"};
这样才是两个字符串,长度为2
数组里只有一个元素,是 i hope tomorrow would be better!
String数组中放的是字符串,你这个只放了一个字符串,所以长度是1
String[] str = new String[]{"hello", "world"};
这样才是两个字符串,长度为2
追问
原来是这样。。。上面那一段话被看成一个整体
那
for (int i=0 ; i<qwe.length ; i++)
{
String qqq=qwe[i];
System.out.print(qqq);
}就执行了qwe[0]这一次呗?
如何我想将qwe[0]倒着输出该如何做,该如何遍历qwe[0]的每一个元素?我能遍历就可以用FOR循环倒着输出了应该。。。
追答
for(int i = qwe.length - 1; i >= 0; i--){//从数组最后一个元素开始循环输出
//语句
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Test {
public static void main(String[] args) {
String qwe="i hope tomorrow would be better!";
char[] c = qwe.toCharArray();
for(int i=c.length-1;i>=0;i--){
System.out.print(c[i]);
}
}
}
你的原因是你建立的是一个String的数组,里面只有一个元素就是你的这句话,你要倒过来输出你得把你的String转换成对应的char数组,代码奉上,供你参考
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你定义的是一个字符串数组,qwe.length是数组的长度
qwe[0].length()是数组中第一个字符串的长度
qwe[0].length()是数组中第一个字符串的长度
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询