import java.util.Arrays;public class Hello { public static void main(String[] args) { int[] scores = new int[]{88, 77, 51, 66}; Score s = new Score(scores); s.printScores();//[ 88, 77, 51, 66 ] scores[2] = 99; s.printScores();//[ 88, 77, 51, 66 ] }}class Score { private int[] scores; public Score(int[] scores) { String scoresString = Arrays.toString(scores);//"[88, 77, 51, 66]" if (scoresString.contains("[")) { scoresString = scoresString.replace("[", "");//"88, 77, 51, 66]" } if (scoresString.contains("]")) { scoresString = scoresString.replace("]", "");//"88, 77, 51, 66" } String[] scoresArray = scoresString.split("\\, "); //将scoresString按逗号分隔为数组scoresArray this.scores = new int[scoresArray.length]; for (int i = 0; i < scoresArray.length; i++) { this.scores[i] = Integer.parseInt(scoresArray[i]); } } public void printScores() { System.out.println(Arrays.toString(scores)); }}
Sign in to make a reply
Young-96