[英]How to determine the last character of a string in which it will be the first character of the next string
我如何获得字符串的最后一个字母,其中它将是下一个字符串的第一个字母。
我知道这与charAt()
但我不知道如何对数组进行处理
比方说,我们有一个这样的输入 x = {"rojan", "yohan", "jamir", "nancy", "nene", "neels"}
输出将是jamir, rojan, nancy, yohan, nene
如果还有其他字符/字母链接到前一个字符,则该程序将忽略。
您可以使用arraylist来获取最终数据,但是在检查每个单词的最后一个字符之前,必须先检查第一个字符,这样您才不会丢失任何数据。如果仅检查最后一个,它将是rojan > nancy > yohan > nene 。所以jamir不在顺序之内,因为我没有寻找不现实的r 。所以可以这样:-
import java.util.ArrayList;
public class NewClass1 {
public static void main (String [] args){
String [] x= {"rojan", "yohan", "jamir", "nancy", "nene","neels"};/*your
array*/
ArrayList<String> newarray = new <String>ArrayList();//resulting array
int j=0;
int i=1;
int check=1;//check 1 means its checking first character
for(;i<x.length-1;i++){//loop will stop at nene
while(newarray.contains(x[i])){//prevent reusing the same word
i++;
}while(newarray.contains(x[j])){//prevent reusing the same word
j++;
}
if(check==1){
if(x[i].charAt(x[i].length()-1)==x[j].charAt(0)){//checking first character
newarray.add(x[i]);
newarray.add(x[j]);
check=2;//setting to 2 after first character found
i=-1;//resetting loop after looking for char
}
}else
if(x[i].charAt(0)==x[j].charAt(x[j].length()-1)){//checking last character
newarray.add(x[i]);
j++;
check=1;//resetting to 1 after last character found
i=-1;
}
}
for(i =0 ;i<newarray.size();i++){//for printing the array
System.out.print(" "+newarray.get(i));
}
}
}
希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.