Discuss / Java / 交作业:不用clone(),用本节内容实现功能

交作业:不用clone(),用本节内容实现功能

Topic source

Young-96

#1 Created at ... [Delete] [Delete and Lock User]
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));    }}

  • 1

Reply