Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Implementation of ArrayLinearList as a LinearList, Slides of Computer Science

An implementation of a linearlist using the arraylinearlist class in java. Constructors, methods such as isempty, size, checkindex, get, indexof, remove, add, and tostring. It also shows examples of creating empty and filled lists, using list methods, and an array of linearlists.

Typology: Slides

2012/2013

Uploaded on 03/27/2013

agarkar
agarkar 🇮🇳

4.3

(26)

380 documents

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
The Class Array, LinearList
General purpose implementation of linear lists.
Unknown number of lists.
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Java Implementation of ArrayLinearList as a LinearList and more Slides Computer Science in PDF only on Docsity!

The Class Array, LinearList

  • General purpose implementation of linear lists.
  • Unknown number of lists.

Create An Empty List

ArrayLinearList a = new ArrayLinearList(100),

b = new ArrayLinearList(),

c;

LinearList d = new ArrayLinearList(1000),

e = new ArrayLinearList(),

f;

Array Of Linear Lists

LinearList [] x = new LinearList [4];

x[0] = new ArrayLinearList(20);

x[1] = new Chain();

x[2] = new Chain();

x[3] = new ArrayLinearList();

for (int i = 0; i < 4; i++)

x[i].add(0, new Integer(i));

The Class ArrayLinearList

/** array implementation of LinearList / package dataStructures; import java.util.; // has Iterator interface import utilities.*; // has array resizing class public class ArrayLinearList implements LinearList { // data members protected Object [] element; // array of elements protected int size; // number of elements in array // constructors and other methods come here }

Another Constructor

/** create a list with initial capacity 10 */ public ArrayLinearList() {// use default capacity of 10 this(10); }

The Method isEmpty

/** @return true iff list is empty */

public boolean isEmpty()

{return size == 0;}

The Method checkIndex

/** @throws IndexOutOfBoundsException when

  • index is not between 0 and size - 1 */ void checkIndex(int index) { if (index < 0 || index >= size) throw new IndexOutOfBoundsException ("index = " + index + " size = " + size); }

The Method get

/** @return element with specified index

  • @throws IndexOutOfBoundsException when
  • index is not between 0 and size - 1 */ public Object get(int index) { checkIndex(index); return element[index]; }

The Method remove

public Object remove(int index) { checkIndex(index); // valid index, shift elements with higher index Object removedElement = element[index]; for (int i = index + 1; i < size; i++) element[i-1] = element[i]; element[--size] = null; // enable garbage collection return removedElement; } Docsity.com

The Method add

public void add(int index, Object theElement) { if (index < 0 || index > size) // invalid list position throw new IndexOutOfBoundsException ("index = " + index + " size = " + size); // valid index, make sure we have space if (size == element.length) // no space, double capacity element = ChangeArrayLength.changeLength1D(element, 2 * size);

Faster Way To Shift Elements 1 Right

System.arraycopy(element, index, element,

index + 1, size - index);

Convert To A String

public String toString() { StringBuffer s = new StringBuffer("["); // put elements into the buffer for (int i = 0; i < size; i++) if (element[i] == null) s.append("null, "); else s.append(element[i].toString() + ", "); if (size > 0) s.delete(s.length() - 2, s.length()); // remove last ", " s.append("]"); // create equivalent String return new String(s); }