java - Array of Doubly Linked Lists -
i create array each element doubly linked list. here's have far:
public arrayoflists() { this.limit = limit; //limit of nodes in each element of listarray listarray = (doublylinkedlist<e>[]) new doublylinkedlist[3]; listarray[0] = new doublylinkedlist<e>(); listarray[1] = new doublylinkedlist<e>(); listarray[2] = new doublylinkedlist<e>(); size = 0; }
i'm not sure if conceptually correct, kind of see 2d array. i'm confused how go adding , removing objects lists stored in array. example,
public void add(e obj) { //some stuff } public void remove(int index) { //some stuff }
could somehow access implemented methods in doublylinkedlist class assist this? thank much.
i'm not sure logic use figure out slot of array want add obj
to, how (after implementing calculatearrayslotsomehow
of course):
public void add(e obj) { int index = calculatearrayslotsomehow(obj); listarray[index].add(obj); }
based on comments, implement calculatearrayslotsomehow
this:
private int calculatearrayslotsomehow(e obj) { // 'count' total number of elements // stored in data structure // 'size' number of array elements // 'limit' number of elements per list int slot = count / limit; if (slot >= size) { throw new indexoutofboundsexception("index: " + slot + ", size: " + size); } return slot; }
and have change add
implementation to:
public void add(e obj) { int index = calculatearrayslotsomehow(obj); listarray[index].add(obj); count++; }
note not thread-safe.
i'm curious trying accomplish, because have feeling might going out of way complicate things.
Comments
Post a Comment