Java 打印字符串数组的替代元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21283254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing alternate elements of a string array
提问by user3220565
I am having trouble executing the code for Printing alternate elements of a string array.
我在执行用于打印字符串数组的替代元素的代码时遇到问题。
I have declared a string "welcome" and I want to read the alternative elements like "W, l, o, etc.
我已经声明了一个字符串“welcome”,我想读取替代元素,如“W、l、o 等”。
//Print alternate elements of a string array.
public class AlternateStringArray {
public static void main(String[] args) {
String str[]= new string[] {"Welcome"};
for (int i=0; i<7; i+2){
System.out.println(str[i]);
}
}
}
Receiving the below error:
收到以下错误:
Type mismatch: cannot convert from string[] to String[]
Type mismatch: cannot convert from String to string
Syntax error on token "+", invalid AssignmentOperator
Please help.
请帮忙。
回答by sadhu
String str[]= new string[] {"Welcome"};
should be
应该
String str[]= new String[] {"Welcome"};
回答by Alexander_Winter
You have an error.
你有一个错误。
String str[]= new string[] --> String str[]= new String[]
回答by Jean Logeart
Try:
尝试:
String str = "Welcome";
char[] strChars = str.toCharArray();
for(int i = 0; i < strChars.length; i += 2) { // go through all the characters
System.out.println(strChars[i]);
}
回答by PaolaG
Replace
代替
String str[]= new string[] {"Welcome"};
to:
String str[]= new String[] {"Welcome"};
For the follow error:
对于以下错误:
Syntax error on token "+", invalid AssignmentOperator
for (int i=0; i<7; i++){
if(i%2 == 0)
{
System.out.println(str[i]);
}
}
}
回答by Vedant Terkar
as every one stated:
正如每一个人所说:
change:
改变:
String str[]= new string[] {"Welcome"};
to:
到:
String str[]= new String[] {"Welcome"};
but why?
但为什么?
so here's my answer:
所以这是我的答案:
java is strictly binded object oriented programming language. everything here is either class, object or method.
java是严格绑定的面向对象编程语言。这里的一切都是类、对象或方法。
In Java, when you do:
在 Java 中,当您执行以下操作时:
String xyz = new String("abc");
You force the creation of a new String object of String class
, this takes up some time and memory at time of creation.
您强制创建一个新的 String 对象String class
,这在创建时会占用一些时间和内存。
but string
on the other hand is treated as literal
; which can't have its objects. and thus the error.
但string
另一方面被视为literal
; 它不能有它的对象。因此错误。
for the second error:
对于第二个错误:
we know that the syntax of for statement is,
我们知道 for 语句的语法是,
for(initialization; condition;
increment/decrement
)
for(initialization; condition;
increment/decrement
)
so as you note third condition isn't satisfiable in i+2
,
so change it to i=i+2
or in short i+=2
.
所以当你注意到第三个条件在 中是不满足的i+2
,所以把它改成i=i+2
或简而言之i+=2
。
thus you're getting that error.
因此你得到了那个错误。
Also you're creating array of String objects. in which str[0]
th element is your string "Welcome"
.
to access character from string at certain position we use charAt(int position)
method for string objects.
此外,您正在创建 String 对象数组。其中str[0]
th 元素是您的 string"Welcome"
。为了在特定位置访问字符串中的字符,我们使用charAt(int position)
字符串对象的方法。
so here to access characters from 0
th string of str
array, we'll use:
所以在这里要访问数组0
字符串中的字符str
,我们将使用:
str[0].charAt(position)
.
str[0].charAt(position)
.
Thus your final working code'll be:
因此,您的最终工作代码将是:
public class AlternateStringArray {
public static void main(String[] args) {
String str[]= new String[] {"Welcome"}; //note change
for (int i=0; i<7; i=i+2){ //note change
System.out.println(str[0].charAt(i)); //note change
}
}
}
also instead of using fixed length i<7
, i'll suggest to get the length of string dynamicallyusing .length()
method. as:
也不是使用固定长度i<7
,我建议使用方法动态获取字符串的长度.length()
。作为:
i<str[0].length()
.
i<str[0].length()
.
so the code now is :
所以现在的代码是:
public class AlternateStringArray {
public static void main(String[] args) {
String str[]= new String[] {"Welcome"}; //note change
for (int i=0; i<str[0].length(); i+=2){ //note change 2
System.out.println(str[0].charAt(i)); //note change
}
}
}
always do understand your code before writing it :-)..
在编写代码之前,请务必了解您的代码:-)..
hope it'll help you.... cheers !!
希望它会帮助你....干杯!
回答by Vinayak Pingale
This is what you are expecting to do.
这就是你期望做的。
String str[] = new String[] { "Welcome" };
for (int i = 0; i < 7; i += 2) {
char c = str[0].charAt(i);
System.out.println(c);
}
But whatever approach you are using is not at all valid for many reasons. As Vakh has said you can use that kind of approach , clean and easy to understand.
但是,由于多种原因,无论您使用哪种方法都根本无效。正如 Vakh 所说,您可以使用那种干净且易于理解的方法。
OR something like this.
或类似的东西。
String str = new String("Welcome");
for (int i = 0; i < str.length(); i += 2) {
System.out.println(i + "::" + str.charAt(i));
}
回答by The Viper
Change your line
改变你的线路
String str[]=new string[]{"Welcome"};
to-
到-
String str[]=new String[]{"Welcome"};
回答by Ankush kumar
public class PrintAlternativeCharacters {
公共类 PrintAlternativeCharacters {
public static void main(String[] args)
{
String str = "ROCKSTAR";
String newStr="";
//EXPECTED OUT PUT :: OR-KC-TS-RA
char[] a = str.toCharArray();
System.out.println("*****************"+a.length);
for(int i = 0 ; i < a.length ; i=i+2)
{
newStr=newStr+str.charAt(i+1)+str.charAt(i);
}
System.out.println(newStr);
}
}
}