Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help in LinkedArrayList

Status
Not open for further replies.

salmantariq

Technical User
Oct 19, 2003
1
CA
hi this is my assignment i ve done part of it it s compiling but i dont get the results tth i want. my 1st array gets filled and everything but when i want to put something greater than the size of 1st array it should go to 2nd arrays tht i made but it doesnt go there. link for assignment info is
I also need some help in Iterator class. i ve posted my LinkedArray class and ListIterator class below can u please help me with tht thanks alot!


import java.util. *;
import java.util.ListIterator.*;

public class LinkedArrayList
{
public ListNode head,tail, Link;
private int size, n;
private Object data;
private int current;
Object Arraylist[], Arraylistone[], Arraylistwo[];
Object first;

public LinkedArrayList(int increment)
{
n = increment;
head = null;
tail = null;
size = 0;
current = 0;
Arraylist = new Object[n+1];
}

public boolean isEmpty()
{
return (size == 0);
}

public int size()
{
return size;
}


public void add(int index, Object o)
{
if(index < n)
{
Arraylistone = Arraylist;
first = Arraylistone;
Arraylistone[index] = o;
size++;
}
else if(index > n)
{
Arraylistwo = Arraylist;
Arraylistone[n] = Arraylistwo;
Arraylistone[index] = Arraylistwo[index-(n-1)];
Arraylistone[index] = o;
size++;
}
}

public Object get(int index)
{
/* if(index < n)
{*/
return (Arraylistone[index]);
/*}
else if(index >= n)
{
return(Arraylistwo[index]);
}
return Arraylistwo[index];*/
}

public Object set(int index, Object o)
{
if(index < n)
{
Arraylistone[index] = o;
}
else if(index > n)
{
Arraylistwo[index] = o;
}
return Arraylistone[index];
}

public Object remove(int index)
{
Object c = Arraylistone[index];
Arraylistone[index] = null;
for(int i=index; i<n; ++i)
{
Arraylistone = Arraylistone[i+1];
}
--size;
return c;
}
}


-----------------------------------------------------------

import java.util. *;
import java.util.ListIterator.*;

public class LinkedArrayListIterator
{
public ListNode head,tail, Link;
private int size, n, index;
private Object data;
private int current;
private Object[] a;
Object Arraylist[], Arraylistone[], Arraylistwo[];

public LinkedArrayListIterator(LinkedArrayList l, Object[] a)
{
a = new Object[n+1];
l = new LinkedArrayList(n+1);
head = null;
tail = null;
size = 0;
current = 0;

}

public boolean hasNext()
{
for(int i = 0; i < Arraylist.length; i++)
{
if(Arraylist != null)
return true;
}
return false;
}

public Object next()
{
Object answer;
if(!hasNext())
throw new NoSuchElementException();
answer = Arraylist[index];
index++;
return answer;
}
}
 
Hi,

if(index < n)
{
Arraylistone = Arraylist;
first = Arraylistone;
Arraylistone[index] = o;
size++;
}
else if(index > n)
{
Arraylistwo = Arraylist;
...
1) what's about if (index == n)?
2) Arraylistwo = Arraylist, that means, the same Arraylist as above (Arraylistone)?
The whole idea looks a bit unusual, and it's getting very unreadable, by leaving the conventions of naming styles.
a) A typ (Object, Listnode, ...) starts with a capital letter (as you did) - beside int, boolean, long, float, double, byte. (Member-)variables are written, starting with a small letter -> arraylist, arrayList, array_list.
b) If you look for a variable name, try to avoid names which are (commonly) used in the java.libs (ArrayList).

Otherwise everybody gets confused.
Programming is hard enough.

I guess you want to do something like this:

Object [] switching_arraylist;
Object [] al_one;
Object [] al_two;
...
// ctor:
public LinkedArrayList (...)
{
// you wanna have two
al_one = new Object [];
al_two = new Object [];
// and use sometimes this, sometimes that:
switching_arraylist = al_one;
}
...
if (index >= n)
{
switching_arraylist = al_two;
}
...

I didn't find the problem with the iterator, but I didn't look intensiv for it.
Perhaps someone else might find out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top