如何删除字符数组中的空位置? 导入java.util.array; 导入java.util.ArrayList; 导入java.util.List; 公共类测试{ 公共静态void main(字符串[]args){ 字符串einnstring=Terminal.askString(“字符串:”); 字符串[]arrayWord=einnstring.split(“”); char[]arrayChar=einnstring.toCharArray(); char[]count=新字符[arrayChar.length]; int[]number=新的int[count.length]; 对于(inti=0;i64&&(byte)arrayChar[i]96&&(byte)arrayChar[i]
我不确定我是否理解代码中的所有内容(例如,内部循环的用途是什么?)。 如果希望在字符串中只保留大写字符,则流有一种可能性。给定一个字符串如何删除字符数组中的空位置? 导入java.util.array; 导入java.util.ArrayList; 导入java.util.List; 公共类测试{ 公共静态void main(字符串[]args){ 字符串einnstring=Terminal.askString(“字符串:”); 字符串[]arrayWord=einnstring.split(“”); char[]arrayChar=einnstring.toCharArray(); char[]count=新字符[arrayChar.length]; int[]number=新的int[count.length]; 对于(inti=0;i64&&(byte)arrayChar[i]96&&(byte)arrayChar[i],java,arrays,Java,Arrays,我不确定我是否理解代码中的所有内容(例如,内部循环的用途是什么?)。 如果希望在字符串中只保留大写字符,则流有一种可能性。给定一个字符串s,您有两个相关的流:s.chars()和s.codePoints(): 公共类StringTest{ 专用静态字符串筛选器UpperCase1(字符串s){ 返回s.chars().filter(字符::isUpperCase) .collect(StringBuilder::new、StringBuilder::appendCodePoint、StringB
s
,您有两个相关的流:s.chars()
和s.codePoints()
:
公共类StringTest{
专用静态字符串筛选器UpperCase1(字符串s){
返回s.chars().filter(字符::isUpperCase)
.collect(StringBuilder::new、StringBuilder::appendCodePoint、StringBuilder::append)
.toString();
}
专用静态字符串筛选器UpperCase2(字符串s){
int[]a=s.chars().filter(Character::isUpperCase.toArray();
返回新字符串(a,0,a.length);
}
专用静态字符串筛选器UpperCase3(字符串s){
返回s.codePoints().filter(字符::isUpperCase)
.collect(StringBuilder::new、StringBuilder::appendCodePoint、StringBuilder::append)
.toString();
}
专用静态字符串筛选器UpperCase4(字符串s){
int[]a=s.codePoints().filter(字符::isUpperCase).toArray();
返回新字符串(a,0,a.length);
}
公共静态void main(字符串[]args){
String s=“AaBbCc我不确定我是否理解代码中的所有内容(例如,内部循环有什么用?)。
如果希望在字符串中只保留大写字符,则流有一种可能性。给定一个字符串s
,您有两个相关的流:s.chars()
和s.codePoints()
:
公共类StringTest{
专用静态字符串筛选器UpperCase1(字符串s){
返回s.chars().filter(字符::isUpperCase)
.collect(StringBuilder::new、StringBuilder::appendCodePoint、StringBuilder::append)
.toString();
}
专用静态字符串筛选器UpperCase2(字符串s){
int[]a=s.chars().filter(Character::isUpperCase.toArray();
返回新字符串(a,0,a.length);
}
专用静态字符串筛选器UpperCase3(字符串s){
返回s.codePoints().filter(字符::isUpperCase)
.collect(StringBuilder::new、StringBuilder::appendCodePoint、StringBuilder::append)
.toString();
}
专用静态字符串筛选器UpperCase4(字符串s){
int[]a=s.codePoints().filter(字符::isUpperCase).toArray();
返回新字符串(a,0,a.length);
}
公共静态void main(字符串[]args){
String s=“AaBbCc您可以按如下方式执行:
public class StringTest {
private static String filterUpperCase1(String s) {
return s.chars().filter(Character::isUpperCase)
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
}
private static String filterUpperCase2(String s) {
int[] a = s.chars().filter(Character::isUpperCase).toArray();
return new String(a, 0, a.length);
}
private static String filterUpperCase3(String s) {
return s.codePoints().filter(Character::isUpperCase)
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
}
private static String filterUpperCase4(String s) {
int[] a = s.codePoints().filter(Character::isUpperCase).toArray();
return new String(a, 0, a.length);
}
public static void main(String[] args) {
String s = "AaBbCcYou can do it as follows:
public class Main {
public static void main(String[] args) {
String einString = "ABCdefgHIJK";
char[] arrayChar = einString.toCharArray();
int nonSmallCharsCount = 0;
char[] nonSmallChars = new char[arrayChar.length];
for (int i = 0; i < arrayChar.length; i++) {
if (!(arrayChar[i] > 96 && arrayChar[i] < 123)) {
nonSmallChars[nonSmallCharsCount++] = arrayChar[i];
}
}
// Create a new char array and copy into it all the non-small characters
char[] out = new char[nonSmallCharsCount];
for (int i = 0; i < out.length; i++) {
out[i] = nonSmallChars[i];
}
System.out.println(out.length);
System.out.println(String.valueOf(out));
}
}
public class Main {
public static void main(String[] args) {
String einString = "ABCdefgHIJK";
char[] arrayChar = einString.toCharArray();
int nonSmallCharsCount = 0;
char[] nonSmallChars = new char[arrayChar.length];
for (int i = 0; i < arrayChar.length; i++) {
if (!(arrayChar[i] > 96 && arrayChar[i] < 123)) {
nonSmallChars[nonSmallCharsCount++] = arrayChar[i];
}
}
// Create a new char array and copy into it all the non-small characters
char[] out = new char[nonSmallCharsCount];
System.arraycopy(nonSmallChars, 0, out, 0, nonSmallCharsCount);
System.out.println(out.length);
System.out.println(String.valueOf(out));
}
}
在上面给出的程序中,您可以使用System.arraycopy
避免最后一个for
循环,如下所示:
public class StringTest {
private static String filterUpperCase1(String s) {
return s.chars().filter(Character::isUpperCase)
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
}
private static String filterUpperCase2(String s) {
int[] a = s.chars().filter(Character::isUpperCase).toArray();
return new String(a, 0, a.length);
}
private static String filterUpperCase3(String s) {
return s.codePoints().filter(Character::isUpperCase)
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
}
private static String filterUpperCase4(String s) {
int[] a = s.codePoints().filter(Character::isUpperCase).toArray();
return new String(a, 0, a.length);
}
public static void main(String[] args) {
String s = "AaBbCcYou can do it as follows:
public class Main {
public static void main(String[] args) {
String einString = "ABCdefgHIJK";
char[] arrayChar = einString.toCharArray();
int nonSmallCharsCount = 0;
char[] nonSmallChars = new char[arrayChar.length];
for (int i = 0; i < arrayChar.length; i++) {
if (!(arrayChar[i] > 96 && arrayChar[i] < 123)) {
nonSmallChars[nonSmallCharsCount++] = arrayChar[i];
}
}
// Create a new char array and copy into it all the non-small characters
char[] out = new char[nonSmallCharsCount];
for (int i = 0; i < out.length; i++) {
out[i] = nonSmallChars[i];
}
System.out.println(out.length);
System.out.println(String.valueOf(out));
}
}
public class Main {
public static void main(String[] args) {
String einString = "ABCdefgHIJK";
char[] arrayChar = einString.toCharArray();
int nonSmallCharsCount = 0;
char[] nonSmallChars = new char[arrayChar.length];
for (int i = 0; i < arrayChar.length; i++) {
if (!(arrayChar[i] > 96 && arrayChar[i] < 123)) {
nonSmallChars[nonSmallCharsCount++] = arrayChar[i];
}
}
// Create a new char array and copy into it all the non-small characters
char[] out = new char[nonSmallCharsCount];
System.arraycopy(nonSmallChars, 0, out, 0, nonSmallCharsCount);
System.out.println(out.length);
System.out.println(String.valueOf(out));
}
}
您可以按如下方式进行操作:
public class StringTest {
private static String filterUpperCase1(String s) {
return s.chars().filter(Character::isUpperCase)
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
}
private static String filterUpperCase2(String s) {
int[] a = s.chars().filter(Character::isUpperCase).toArray();
return new String(a, 0, a.length);
}
private static String filterUpperCase3(String s) {
return s.codePoints().filter(Character::isUpperCase)
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
}
private static String filterUpperCase4(String s) {
int[] a = s.codePoints().filter(Character::isUpperCase).toArray();
return new String(a, 0, a.length);
}
public static void main(String[] args) {
String s = "AaBbCcYou can do it as follows:
public class Main {
public static void main(String[] args) {
String einString = "ABCdefgHIJK";
char[] arrayChar = einString.toCharArray();
int nonSmallCharsCount = 0;
char[] nonSmallChars = new char[arrayChar.length];
for (int i = 0; i < arrayChar.length; i++) {
if (!(arrayChar[i] > 96 && arrayChar[i] < 123)) {
nonSmallChars[nonSmallCharsCount++] = arrayChar[i];
}
}
// Create a new char array and copy into it all the non-small characters
char[] out = new char[nonSmallCharsCount];
for (int i = 0; i < out.length; i++) {
out[i] = nonSmallChars[i];
}
System.out.println(out.length);
System.out.println(String.valueOf(out));
}
}
public class Main {
public static void main(String[] args) {
String einString = "ABCdefgHIJK";
char[] arrayChar = einString.toCharArray();
int nonSmallCharsCount = 0;
char[] nonSmallChars = new char[arrayChar.length];
for (int i = 0; i < arrayChar.length; i++) {
if (!(arrayChar[i] > 96 && arrayChar[i] < 123)) {
nonSmallChars[nonSmallCharsCount++] = arrayChar[i];
}
}
// Create a new char array and copy into it all the non-small characters
char[] out = new char[nonSmallCharsCount];
System.arraycopy(nonSmallChars, 0, out, 0, nonSmallCharsCount);
System.out.println(out.length);
System.out.println(String.valueOf(out));
}
}
在上面给出的程序中,您可以使用System.arraycopy
避免最后一个for
循环,如下所示:
public class StringTest {
private static String filterUpperCase1(String s) {
return s.chars().filter(Character::isUpperCase)
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
}
private static String filterUpperCase2(String s) {
int[] a = s.chars().filter(Character::isUpperCase).toArray();
return new String(a, 0, a.length);
}
private static String filterUpperCase3(String s) {
return s.codePoints().filter(Character::isUpperCase)
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
}
private static String filterUpperCase4(String s) {
int[] a = s.codePoints().filter(Character::isUpperCase).toArray();
return new String(a, 0, a.length);
}
public static void main(String[] args) {
String s = "AaBbCcYou can do it as follows:
public class Main {
public static void main(String[] args) {
String einString = "ABCdefgHIJK";
char[] arrayChar = einString.toCharArray();
int nonSmallCharsCount = 0;
char[] nonSmallChars = new char[arrayChar.length];
for (int i = 0; i < arrayChar.length; i++) {
if (!(arrayChar[i] > 96 && arrayChar[i] < 123)) {
nonSmallChars[nonSmallCharsCount++] = arrayChar[i];
}
}
// Create a new char array and copy into it all the non-small characters
char[] out = new char[nonSmallCharsCount];
for (int i = 0; i < out.length; i++) {
out[i] = nonSmallChars[i];
}
System.out.println(out.length);
System.out.println(String.valueOf(out));
}
}
public class Main {
public static void main(String[] args) {
String einString = "ABCdefgHIJK";
char[] arrayChar = einString.toCharArray();
int nonSmallCharsCount = 0;
char[] nonSmallChars = new char[arrayChar.length];
for (int i = 0; i < arrayChar.length; i++) {
if (!(arrayChar[i] > 96 && arrayChar[i] < 123)) {
nonSmallChars[nonSmallCharsCount++] = arrayChar[i];
}
}
// Create a new char array and copy into it all the non-small characters
char[] out = new char[nonSmallCharsCount];
System.arraycopy(nonSmallChars, 0, out, 0, nonSmallCharsCount);
System.out.println(out.length);
System.out.println(String.valueOf(out));
}
}
Array是一种严格的数据类型,您需要的是一个类似于数组的动态列表,您可以在其中删除元素。如果您想使用Array,则需要移动元素,同时单独维护一个计数,该计数将给出数组中有效字符的长度。如果IF
子句是tr,为什么不打印字母呢ue,如果为false,则不打印?数组是一种严格的数据类型,您需要的是一个类似于动态数组的列表,可以在其中删除元素。如果您希望使用数组,则需要移动元素,同时单独维护一个计数,该计数将给出数组中有效字符的长度。如果您的if
子句为true,如果为false则不打印?