java筆試題(一)
阿新 • • 發佈:2022-03-27
import java.util.Arrays; public class Javatest75 { /** *筆試題 * 練習1:刪除字串下標是3的倍數的字元 ,s = "aabbcdeefg",輸出abcdef * [a, a, b, b, c, d, e, e, f, g] * abcdef */ public static void main(String[] args) { String s = "aabbcdeefg"; //將字串轉換成字元陣列 char[] arr = s.toCharArray();//列印字元陣列 System.out.println(Arrays.toString(arr)); //遍歷字元陣列,將下標是3的倍數的字元替換成‘ ’ for (int i = 0; i < arr.length; i++) { if(i % 3 == 0){ arr[i] = ' '; } } StringBuilder s1 = new StringBuilder(); //遍歷字元陣列,將不是空格的字元按順序一次新增至s1for (int j = 0; j < arr.length; j++) { if(arr[j] == ' '){ continue; } s1.append(arr[j]); } //列印s1 System.out.println(s1.toString()); } }