c# - Change value of an index in an array, shift indexes starting at old index to the right, while leaving all indexes to the left intact -
i have array , have new value want insert array @ index less left of it, , greater right of it. care 10 indexes, old values pushed beyond index [9] shall destroyed.
i tried accomplishing way:
private double[] scores = {1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1}; public void newscore(double score){ (int = 0; < scores.length; i++){ if (score > this.scores[i]){ array.copy(this.scores, i, this.scores, + 1, this.scores.length - i); this.scores[i] = score; break; } } } newscore(0.75); //desired result: {1.0, 0.9, 0.8, 0.75, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2};
my goal 0.75 inserted between 0.8 , 0.7 while values less 0.75 shift right , old value @ score[9]
disappear.
i having issues array.copy();
method. there better way accomplish trying do, or have made simple mistake cannot find?
i have searched other solutions ones have found shift indexes either right or left instead of values less value inserted.
replace this.scores.length - i
this.scores.length - - 1
.
you have mistake in sample: 0.10 = 0.1, it's poor score.
original score array {1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1}
Comments
Post a Comment