java - I have to write my own trimtoSize() method that creates a new array -
i've been give prewritten program exception of few methods, 1 of being trimtosize()
. know exists have write own. have make there empty space within array have create new array using same values, , make size exact. i've been able compare sizes using size()
, data.length
i'm not sure how create new array used since function doesn't return anything(it's void).
here's have far.
public void trimtosize(){ array<integer> temp = new array<integer>(); if(size() < data.length){ int max = size(); for(int i=0; i<size();i++){ temp.add(data[i]); } }}
as said before don't know how return temp, , keep same max capacity same before because created using constructor. there way change capacity without changing constructor?
i can't bee sure without seeing rest of code, itappears part of class , in class integer array named data... if assumtion true, should work..
public void trimtosize() { int size = 0; for(int = 0; < this.data.length; i++) { if(this.data[i] != null) { size++; } } interger[] temp = new interger[size]; for(int = 0, j = 0; < this.data.length; i++) { if(this.data[i] != null) { temp[j] = this.data[i]; j++; } } this.data = temp; }
Comments
Post a Comment