Singly Linked LIst and Objects Newbie Question

  • Follow


Hello all,I am creating a linked list module, and am currently developing thelinked list insert section of my program.  I am still testing thelogic but I've encountered a runtime errorIn short, I create a tempList of LinkedList class, I insert a valueusing the insert method and when it comes back,  the value is notshown in the tempList like I thought it should have.I think I'm calling it correctly to be initialized, and I'm surprisedthe debugger I'm using hasn't blown up by now, I've ran it so manytimes.  I'm not understanding why the value of '3' is not reflected inthe tempList's data after calling the insert method.  It returns asnull.  Because it's null that's why I encounter a runtime error forthe 2nd insertation.  :(I included a copy of my program, stripped of all extraneous lines.  Iapology in advance for the rough style of my program.  I tried veryhard to put it into the form that SSCCE dictates.  I'm not concernedas much as to whether the linked list's logic is working as I am withthe technical aspect of objects and how to manipulate them .(objects...I dream about this stuff now lol.)Any help is appreciated,-timport java.lang.*;public class MyProg{   public static void main(String[] arguments) {      int size = 10;  //size to be read in from input of file later      LinkedList [] bucketArray = new LinkedList[ size ];      Integer three = new Integer(3);      Integer four = new Integer(4);      LinkedList tempList = new LinkedList();      tempList.insert ( three, 1 );      tempList.insert ( four, 2 );      bucketArray[0] = tempList;      }   //end of main}class LinkedList{   private Node head;   private int length;    public LinkedList() {    this.head = null;    this.length = 0;    }public Object getItemAtPosition(int position) {   if (position<0 || position >= this.length){      return null;   }   Node curr = this.head;      for (int i=0;i<position;i++){         curr = curr.getNext();      }   return curr.getData();}public Node getNodeAtPosition(int position) {   if (position<0 || position > this.length){      return null;   }   Node curr = this.head;   for (int i=0;i < position;i++){      curr = curr.getNext();    // <-- null pointer exception   }   return curr;}public boolean insert (Object data,int position){   if (position<0 || position-1 > this.length ){      return false;   }   Node previous = this.getNodeAtPosition(position-1);   Node newNode = new Node(data);   if (position - 1 == 0) {  //list is empty      newNode.setNext(null);      newNode.setData(data);   }   else {      newNode.setNext(previous.getNext());      previous.setNext(newNode);   }   length++;   return true;   }}class Node {   private Object data;   private Node next;   public Node(Object data){       this.data = data;   }   public Node(Object data,Node next){        this.data = data;        this.next = next;   }   public void setNext(Node next){      this.next = next;   }   public void setData(Object data){      this.data = data;   }   public Node getNext(){      return this.next;   }   public Object getData(){      return this.data;   }}
0
Reply mchew02 (18) 9/30/2007 10:51:50 AM

I'm sorry, when I edited the program above, I left out the last curlybrace.  If you insert it (the '}' thingie) at the end of the code,then it will compile.Sorry about that.  :x-t
0
Reply Taria 9/30/2007 10:59:04 AM


I might be missing something totally obvious here, but where in yourcode are you setting the 'head' of the list? This is something thatshould probably go into the block where you check for the 'firstvalue'.Another sidenote: you should stick to the standard conventions of CS,like that the first element of a list has the index '0', not '1' as inyour case (see for instance arrays, the java.util collections, ...)/philipp
0
Reply Philipp 9/30/2007 11:11:09 AM

Ack, and one other thing.  You need to include this line at the top:import java.util.*;I'm so sorry, the mouse got stuck and it didn't copy over quite right.-tThe...cat made me do it, yea yea.  (I'd rather blame the cat thanme!)  :p
0
Reply Taria 9/30/2007 11:14:07 AM

Philipp Leitner wrote:> I might be missing something totally obvious here, but where in your> code are you setting the 'head' of the list? This is something that> should probably go into the block where you check for the 'first> value'.> > Another sidenote: you should stick to the standard conventions of CS,> like that the first element of a list has the index '0', not '1' as in> your case (see for instance arrays, the java.util collections, ...)What do you mean by "CS"?Java arrays and implementations of java.util.List are zero-based in fact, not by convention.  java.sql.PreparedStatement and ResultSet count parameters columns respectively from one, not zero.  I'm not sure one can assert the existence of a convention.It is enough, though, that the fact of arrays and java.util.Lists is that they index from zero.  The advice to do likewise in classes that extend or wrap arrays or java.util.List is sound.  Among other things, it eliminates the confusing and error-prone offsets by one that abound in the OP's code.To the OP: your main() method ought to do something to display or otherwise prove that the contents of your list are what you expect them to be.-- Lew
0
Reply Lew 9/30/2007 4:51:46 PM

Lew wrote:> Philipp Leitner wrote:>> Another sidenote: you should stick to the standard conventions of CS,>> like that the first element of a list has the index '0', not '1' as in>> your case (see for instance arrays, the java.util collections, ...)> > What do you mean by "CS"?Coding Standard seems more likely than Computer Science.Arne
0
Reply UTF 9/30/2007 7:01:04 PM

On Sep 30, 6:51 am, Lew <l...@lewscanon.com> wrote:> Philipp Leitner wrote:> > I might be missing something totally obvious here, but where in your> > code are you setting the 'head' of the list? This is something that> > should probably go into the block where you check for the 'first> > value'.Ok, I did that but by doing so, I had to change the private access ofhead to protected for it to work.  At this point, I'm unsure and tootired to think whether this is accepted for the 'data hiding' theorypart of Javs.  Professors are adamant throughout my courses thateverytihng is declared as private.>> > Another sidenote: you should stick to the standard conventions of CS,> > like that the first element of a list has the index '0', not '1' as in> > your case (see for instance arrays, the java.util collections, ...)>> What do you mean by "CS"?>I think he means:  CS = computer science.>> To the OP: your main() method ought to do something to display or otherwise> prove that the contents of your list are what you expect them to be.>> --> LewNoted...the next time I post something of this nature, I shall includeexternal debugging statements within the code.Thanks guys, for your input.  :)-t
0
Reply Taria 9/30/2007 7:03:26 PM

Taria,There are many mistakes you make. Start out with what Philippsuggested.Your insert method should set the head, because that is the onlyadding method.Your getItemAtPosition() will generate a NullPointerException if yourhead is null.I take it your indexing range is [0, length-1].getNodeAtPosition() uses [0, length-1] but length is allowed.insert() uses [0, length-1] but length and length+1 are allowedYou do nothing with newNodeif (position - 1 == 0) {  //list is empty      newNode.setNext(null);      newNode.setData(data);}should probably beif (previous == null) {    newNode.setNext(null);    newNode.setData(data);    this.head = newNode;}Good luck,xen.
0
Reply xen 10/2/2007 3:36:37 AM

Also, Taria....>import java.lang.*;You never need to import java.lang.*  ;)And perhaps nice to know, since java 1.5 you don't need to create anInteger object if you want to pass an int as a parameter, it iscreated for you.And perhaps also nice to know, a good coding style is to put the 'mostsignificant' argument first in a function definition.For example, if your method copies part of an array, you'd firstspecify the array, and then the length, as inbyte[] copyOf(byte[] original, int newLength)If your method fills a part of an array with a value, you'd firstspecify the array, then the range, and then the value, as invoid fill(byte[] a, int fromIndex, int toIndex, byte val)Likewise, you'll eventually start to write insert() methods likevoid insert( int position, Object value)instead ofvoid insert(Object value, int position)It's a bit like Japanese. Boku wa, ima koko de kimi ni okane o ageru.I, now, here, to you, money, am going to give.xen.
0
Reply xen 10/2/2007 7:27:07 AM

On Oct 1, 9:27 pm, xen <x...@rotmail.nl> wrote:> Also, Taria....>> >import java.lang.*;>> You never need to import java.lang.*  ;)>> And perhaps nice to know, since java 1.5 you don't need to create an> Integer object if you want to pass an int as a parameter, it is> created for you.>> And perhaps also nice to know, a good coding style is to put the 'most> significant' argument first in a function definition.>> For example, if your method copies part of an array, you'd first> specify the array, and then the length, as in>> byte[] copyOf(byte[] original, int newLength)>> If your method fills a part of an array with a value, you'd first> specify the array, then the range, and then the value, as in>> void fill(byte[] a, int fromIndex, int toIndex, byte val)>> Likewise, you'll eventually start to write insert() methods like>> void insert( int position, Object value)>> instead of>> void insert(Object value, int position)>> It's a bit like Japanese. Boku wa, ima koko de kimi ni okane o ageru.> I, now, here, to you, money, am going to give.>> xen.THank you Xen, I really appreciate your comments.  The classes that Itake in school are so large that the programs I submit are not givenback with comments on how to improve them.  Don't get me wrong, theprofessors I have currently and in the past are great but they areoverloaded (imho) where they are looking over say 25 programs perperson, 60 students (2 classes) + a load of other classes they'reteaching (my first ICS class i had.)  Even with a TA, Iknow they'reoverloaded.So, your comments are greatly appreciated and I will try to strive toincorporate good programming style hints+ whatever else I need towrite coherant material.Thanks to a few ppl on these forums, Lasse, Daniel and Roedy coming toimmediate recall, which helped me in completing this assignment.  It'sdue in a couple days and now at least I can try refining my program(instead of frantically debugging).  I can say there were quite a fewparamount concepts that I've learned with this assignment that failedto stick with me in previous classes.Now that this is done, I'm not sure what to do with my time.  (wellall 1 day of freedom.)  I was considering on implementing a red blacktree algorithm for my personal project to continue learning how to useobjects, and to strengthen my understanding of it because it'll be onthe next midterm.  I have a bad feeling though, it sounds hard...somaybe not.  :pI understand Japanese actually.  :)  Exposed to it everyday so Irecognize the structure...it's an interesting analogy and one I willremember.THanks again,..everyone!-t
0
Reply Taria 10/2/2007 8:39:00 AM

>Now that this is done, I'm not sure what to do with my time.  (well>all 1 day of freedom.)  I was considering on implementing a red black>tree algorithm for my personal project to continue learning how to use>objects, and to strengthen my understanding of it because it'll be on>the next midterm.  I have a bad feeling though, it sounds hard...so>maybe not.  :p>>I understand Japanese actually.  :)  Exposed to it everyday so I>recognize the structure...it's an interesting analogy and one I will>remember.Hm it seems that my algorithm and datastructure knowledge is reallylimited. I've studied computer science for 3 years, but never got tothe advanced parts because of health problems. There was only anintroductory course on datastructures and algorithms in the firstyear. I had never even heard of red-black trees or 2-3-4 trees or anyother of the optimizations.If you want to study objects I think the most interesting part ispolymorphism. For example, some base abstract class defines some setof methods including complex methods that operate on the simpler ones.Then you subclass this base class and provide your own implementationfor the the simple methods. The complex methods that are already therenow operate on YOUR simple methods instead of those of the base class,so you get their functionality for free.That kind of stuff. You could study the Java Collection Framework andits source, to see how Interface builds upon Interface and subclassbuilds upon abstract class. But perhaps you've already done all that.But then there's nothing more you need to learn about objects, Iguess.This is also quite cool btw. You can't have nested methods in Java,ie. you can't define a method inside a method. You also cannot definea static class and use that, although the compiler can and doessometimes. But you CAN define a class inside a method if youimmediately instantiate it, and have the class implement a 'nested'method.So for example you want to write an assertion that evaluates a complexexpression. You could define a new method:boolean method1_checkAssert(int a, Object b) {    // calculated boolean value   return resut;}void method1(int a, Object b) {    // check whether the preconditions are satisfied and throw    // an AssertionError otherwise:   assert method1_checkAssert(a,b): "" + a + " must be equal to " + b;}But if the checkAssert method is not so big, you might do:void method1(int a, Object b) {    assert new Object() {        boolean eval(int a, Object b) {            // calculate boolean value            return result;        }    }.eval(a,b): "" + a + " must be equal to " + b;}The drawback is that this creates an object but you would turnassertions off anyway in production code. Note that you can't use anyof the method's variables unless you make them final so you musteither make them final or pass them as parameters to eval()hope you find it interesting,xen.
0
Reply xen 10/2/2007 11:49:03 PM

>So for example you want to write an assertion that evaluates a complex>expression. You could define a new method:I forgot to mention that the expression you'd want to evaluate is notreally an expression but something you need multiple statements for tocalculate, so you can't fit it into the assert statement just likethat.
0
Reply xen 10/2/2007 11:53:37 PM

xen <xen@rotmail.nl> writes:>you need multiple statements for to calculate, >so you can't fit it into the assert statementpublic class Main{ public static void main( final java.lang.String[] args )  { assert new java.util.concurrent.Callable<java.lang.Boolean>()    { public java.lang.Boolean call()      { int a = 7;        ++a;        return a == 9; }}.call();     java.lang.System.out.println( "ok." ); }}Exception in thread "main" java.lang.AssertionError        at Main.main(Main.java:3)
0
Reply ram 10/3/2007 12:15:52 AM

Stefan Ram wrote:> xen <xen@rotmail.nl> writes:>> you need multiple statements for to calculate, >> so you can't fit it into the assert statement> > public class Main> { public static void main( final java.lang.String[] args )>   { assert new java.util.concurrent.Callable<java.lang.Boolean>()>     { public java.lang.Boolean call()>       { int a = 7;>         ++a;>         return a == 9; }}.call(); >     java.lang.System.out.println( "ok." ); }}> > Exception in thread "main" java.lang.AssertionError>         at Main.main(Main.java:3)You're scaring me.Actually, I like this idiom a lot, except for what looks suspiciously like abuse of the purpose of assertions.  The use of an anonymous class to provide closure-like action is a cool, if at first confusing-looking way to do things.Java programmers must design their assertions with the thought that they will likely be turned off in production firmly in mind.Assertions exist primarily to enforce algorithmic invariants, although their power admits of expanded usage based on that theme.  Their highest, best use is as part of a test strategy and framework.Assertions are very poorly suited for run-time data validation.  The rule of thumb is to use assertions on private and package-private data and methods; use conditional branching and exceptions on protected and public data and methods.Note that failed assertions throw an Error, "a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch."<http://java.sun.com/javase/6/docs/api/java/lang/Error.html>Assertions are much more heavyweight and specialized in purpose than validation logic and exceptions are.-- Lew
0
Reply Lew 10/3/2007 1:48:06 AM

On 3 Oct 2007 00:15:52 GMT, ram@zedat.fu-berlin.de (Stefan Ram) wrote:>xen <xen@rotmail.nl> writes:>>you need multiple statements for to calculate, >>so you can't fit it into the assert statement>>public class Main>{ public static void main( final java.lang.String[] args )>  { assert new java.util.concurrent.Callable<java.lang.Boolean>()>    { public java.lang.Boolean call()>      { int a = 7;>        ++a;>        return a == 9; }}.call(); >    java.lang.System.out.println( "ok." ); }}>>Exception in thread "main" java.lang.AssertionError>        at Main.main(Main.java:3)>Yes, generics, all the way! Only drawback is that you can't call itwith parameters. And it seems a bit unnecessary and well, useless. Whyimplement an interface if it has no use AT ALL? It's not as if you'regonna pass this object to anyone. Just a label to signify yourintention with this object? Well then you would better define your ownempty interface that doesn't place any restriction on the call method.It surpirised me that you don't have to catch the Exception that'sdefined on the call method in Callable; apparently such exceptionsonly take effect if the implementing class specifies it.
0
Reply xen 10/3/2007 2:14:01 AM

On Tue, 02 Oct 2007 21:48:06 -0400, Lew <lew@lewscanon.com> wrote:>You're scaring me.>>Actually, I like this idiom a lot, except for what looks suspiciously like >abuse of the purpose of assertions.  The use of an anonymous class to provide >closure-like action is a cool, if at first confusing-looking way to do things.>>Java programmers must design their assertions with the thought that they will >likely be turned off in production firmly in mind.>>Assertions exist primarily to enforce algorithmic invariants, although their >power admits of expanded usage based on that theme.  Their highest, best use >is as part of a test strategy and framework.>>Assertions are very poorly suited for run-time data validation.  The rule of >thumb is to use assertions on private and package-private data and methods; >use conditional branching and exceptions on protected and public data and methods.>>Note that failed assertions throw an Error, "a subclass of Throwable that >indicates serious problems that a reasonable application should not try to catch."><http://java.sun.com/javase/6/docs/api/java/lang/Error.html>>>Assertions are much more heavyweight and specialized in purpose than >validation logic and exceptions are.I'm currently using assertions that call an evaluation function touncover programming errors that originate outside of the package,because it will be so easy to turn this checking off when it is nolonger necessary. Eventually it won't be possible for a user togenerate such errors but right now I sometimes generate them myself inmy 'debugging console' so it would be better if they were just REs.If the package were part of a library used by other applications, Iguess I'd have to convert them to regular RuntimeExceptions...I actually sometimes catch an assertion so that I can print debugginginformation and then rethrow it.
0
Reply xen 10/3/2007 2:43:44 AM

xen <xen@rotmail.nl> writes:>>    { public java.lang.Boolean call()>Yes, generics, all the way! Only drawback is that you can't call it  When I wrote this, I was not aware that you actually  had posted something like this in another posting.  So, I just wanted to show, that in Java, an expression  might contain statements.  You are right that the interface is not necessary:  I erroneously believed that it was nessary.>It surpirised me that you don't have to catch the Exception that's>defined on the call method in Callable; apparently such exceptions>only take effect if the implementing class specifies it.  It seems that an extension of a abstract method can restrict  the exceptions thrown. This seems to be suggested byhttp://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.3.1  It only seems to be required that      �a method declaration must not have a throws clause that      conflicts (�8.4.6) with that of any method that it overrides;�http://java.sun.com/docs/books/jls/third_edition/html/interfaces.html#9.4  Also, see the example:http://java.sun.com/docs/books/jls/third_edition/html/interfaces.html#9.4.3.1  Here is another example by me:interface Example { void run() throws java.lang.Throwable; }class Example1 implements Example{ public void run(){ java.lang.System.out.println( "Example" ); }}public class Main{ public static void main( final java.lang.String[] args )  { new Example1().run(); }}Example
0
Reply ram 10/3/2007 2:45:20 AM

On 3 Oct 2007 02:45:20 GMT, ram@zedat.fu-berlin.de (Stefan Ram) wrote:>      �a method declaration must not have a throws clause that>      conflicts (�8.4.6) with that of any method that it overrides;�Aha. More precisely:�A method that overrides or hides another method (�8.4.8), includingmethods that implement abstract methods defined in interfaces, may notbe declared to throw more checked exceptions than the overridden orhidden method.�http://java.sun.com/docs/books/jls/third_edition/html/classes.html#308526So it can't become more, but it can become less.
0
Reply xen 10/3/2007 4:44:18 AM

On Oct 2, 10:39 am, Taria <mche...@hotmail.com> wrote:> Now that this is done, I'm not sure what to do with my time.  (well> all 1 day of freedom.)Roedy has a list of potential student projects on his site. I don'tknow if some are one day projects.But if you are really interested, why not start a larger project onthe side, and work on it whenever you find some time? The fewprogramming exercises you typically get at school don't make you aprogrammer, they are to show concepts and keep you busy.
0
Reply Hunter 10/3/2007 6:07:24 AM

Hunter Gratzner wrote:> On Oct 2, 10:39 am, Taria <mche...@hotmail.com> wrote:>> Now that this is done, I'm not sure what to do with my time.  (well>> all 1 day of freedom.)> > Roedy has a list of potential student projects on his site. I don't> know if some are one day projects.> > But if you are really interested, why not start a larger project on> the side, and work on it whenever you find some time? The few> programming exercises you typically get at school don't make you a> programmer, they are to show concepts and keep you busy.> Not only that, but they tend to teach you how to write bad code, and clarify it with redundant comments.  Just wrote a blog about that actually...<http://virtualinfinity.net/wordpress/uncategorized/2007/10/02/writing-as-an-artform/>-- Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
0
Reply Daniel 10/3/2007 6:14:43 AM

19 Replies
94 Views

(page loaded in 0.253 seconds)


Reply: