Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I am trying to write a linked list using an array-based linked list. So far i have the code to create the list and insert an element but i get an exception when the list is not empty and has elements in it. Below is the code. Also how can i print the elements of the list to see if they are actually sorted?

EDIT: The code works now, i just cant get to show the elements stored in the list.

package listpackage;

import java.io.IOException;

public class ArrayLL {
private int MAX_CAP = 100;
private ANode[] list;
private int size;

public ArrayLL(){
    list = new ANode[MAX_CAP];
    list[list.length-1] = new ANode(null, -1);
    for(int i = 0; i < list.length-1; i++){
        list[i] = new ANode(null, i+1);
    }
    size = 0;
}

public void addElem(String s) throws IOException{
    if(size == 0){
        ANode a = new ANode(s, -1);
        list[0] = a;
        size++;
    }else if(size == MAX_CAP + 1){
        throw new IOException("List is full");
    }else{
        for(int i = 0; i < this.getSize()-1; i++){
            if(list[i].getData().compareTo(s) > 0){
                for(int j = this.getSize(); j > i; j--){
                    list[j] = list[j+1];
                }
                ANode b = new ANode(s,i+1);
                list[i] = b;
                size++;
                break;
            }else{
                ANode c = new ANode(s,-1);
                list[size+1] = c;
                size++;
            }
        }
    }
}

public int getSize(){
    return size;
}

}

class ANode{
private String data;
private int link;

public ANode(String d, int l){
    data =  d;
    link = l;
}

public String getData(){
    return data;
}

public int getLink(){
    return link;
}
}
share|improve this question

closed as off topic by Winston Ewert Feb 17 '13 at 4:28

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Welcome to Code Review, Leon! Unfortunately, questions about code that doesn't work are off-topic here. This question can only remain open if you edit it, turning it into a code review request as explained in the faq. –  codesparkle Feb 16 '13 at 23:34
1  
I edited the code and now it works. Please see edit. –  Leon Feb 16 '13 at 23:48
1  
It is still not working, your question goes for some none working functionality. –  tb- Feb 17 '13 at 4:05
add comment

1 Answer 1

To be honest, there is a lot of work to do here. If you are learning the language, please try to do it in small steps instead of trying such an example.

I will just highlight some parts, these is not a complete answer.

If you want to have a sorted data structure, use a Tree. Do not try to invent your own implementation of a well known data structure. Use existing ones. You will most probably not be more clever than the combined experience of some good developers.


If you want to create a list in Java, it could be a good idea to implement the Listinterface (But I would suggest to use a tree here, not a list)


public void addElem(String s) throws IOException{

IOExceptionis for input/output like for data reading/writing. If you really need to throw an exception, you could use a IllegalStateException here.


for(int i = 0; i < this.getSize()-1; i++){

This will always be a loop without any looping. size is 1, 1-1=0, 0<0 is false. Loop is not executed, nothing happens. You will always set only one element (the first) in the array.


Also how can i print the elements of the list to see if they are actually sorted?

You could implement a toString() method. In both classes.


if(list[i].getData().compareTo(s) > 0){

If you want to compare two items, you have should implement the Comparable interface and the compareTo method then.


     for(int i = 0; i < this.getSize()-1; i++){
        ....
        }else{
            ANode c = new ANode(s,-1);
            list[size+1] = c;
            size++;
        }
    }

This will probably have an endless loop (until list access throws an ArrayIndexOutOfBoundsException). You modify the loop termination. You should not to such things.


The naming is awful. Try to use "speaking" names. If you have a class/object which should represent a ordered list, name it OrderedList. And so on.


In general, there are a lot more bad things happening here. I really suggest to just use a tree. And if you plan to do some learning, try smaller steps and exercises, probably read some book about Java in general and study some given List implementations (Not the ones from Java, some from educational books).

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.