• Post Reply Bookmark Topic Watch Topic
  • New Topic

A simple question about Java Generic  RSS feed

 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, I'm working with Doubly Linked Lists and using Java Generics..

My nodes looks like this:


My list of Nodes looks like this:



As you can see, as arguments they get "E o"...
I need to write a program, which from the main function asks the users how long is the list, and after they type it's length, I ask them to start typing the elements (integers)...and this is how my main method is written, but I can't seem to make it work, specialy when I call the "insLast" method,I guess it's because the arguments i'm giving to the function...

Any ideas on how to read the elements and write them into the list?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What error are you getting? I suspect you get a compile error in this line:

Note that Integer.parseInt(...) returns an int, not an Integer. It will work better if you use Integer.valueOf(...) instead:
 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Getting this:

 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error in the tooltip tells you what the problem is. When it says 'cannot find symbol' it means that you'd used a name that it doesn't recognise (i.e. you haven't defined anything with that name). In your case, the code that is highlighted is the method name 'instLast'.

If you look closely at your actual method definition you'll see that it has a different name.
 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohhh, damn that extra t lol...
I have a question guys... what does that extra string do?
This line:



Can you please explain me what these three lines exactly do, I want to understand it deeply, not just learn it as it is...
 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having a bit of a trouble again guys...
You already saw how I read my elements...
1. first I get the length of the list.
2. then i start reading the elements one by one (with a space between them).

But I have a small problem when printing the array (just to make sure I red it correctly)
This is the function to print the list:



and this is how I call it from the main function


But when It prints the arrays, first it shows:
null
null
then it prints the elements.... for example this is what it prints for an array of 5 elements (1,2,3,4,5):
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bashkim Ukshini wrote:Ohhh, damn that extra t lol...
I have a question guys... what does that extra string do?
This line:



Can you please explain me what these three lines exactly do, I want to understand it deeply, not just learn it as it is...


I don't know what you mean by the extra String. Do you mean the String literal that is being passed as a parameter to the split method? If so, check the Javadoc for that method and it will explain it all.

String.split(String)
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bashkim Ukshini wrote:I'm having a bit of a trouble again guys...
You already saw how I read my elements...
1. first I get the length of the list.
2. then i start reading the elements one by one (with a space between them).

But I have a small problem when printing the array (just to make sure I red it correctly)
This is the function to print the list:



and this is how I call it from the main function


But when It prints the arrays, first it shows:
null
null
then it prints the elements.... for example this is what it prints for an array of 5 elements (1,2,3,4,5):


I think you're going to have to post all of your code for us to answer that question. Nowhere in the code you've posted does head get initialised. If you show all of the code then we'll be able to see what's going on.
 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure guys, here you go:

 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S: I'm also checking the SIZE of the elements there are in the list, it's correct.. it shows me the exact number, but why the TWO EXTRA null null ?I really don't get it :/
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bashkim Ukshini wrote:P.S: I'm also checking the SIZE of the elements there are in the list, it's correct.. it shows me the exact number, but why the TWO EXTRA null null ?I really don't get it :/

If you mean the value of your size variable matches the number of nodes that you add to the list, that may well be true, but it doesn't match the number of nodes that are actually in the list.

How many nodes are in the list after line 110 is executed ? Here's a clue - the answer isn't zero. Have a look at the constructor.
 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohh... that is my deafult constructor... so I need to have ONE node when my default constructor is called??

I tried this, but I still get the same, but this time just ONE null...



I believe this is the solution:


If yes, can you please shortly explain me why?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bashkim Ukshini wrote:I believe this is the solution:


If yes, can you please shortly explain me why?

It is (almost) the correct solution, but why do you think it is the correct solution ? And can you work out why I said almost. Think about what's going to happen when lines 6 and 7 are executed.
 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I don't need them at all... right?
It should only look like this:
 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay guys, everything is working perfectly... I just need to know how to print my list's element with a space between them...
The assingment was so that after I get the list from the user, I would check each element of it, and see if it's element (integer) is ODD or EVEN, and place that element in a new DLL (if it's odd, in the list of odd elements, if it's even, in the list of even elements)..

But now I need to print them both out, but not with new lines but with a SPACE between them... anyone could give me an idea please?
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The new-lines happen automatically as part of System.out.println. So if you don't want them, use System.out.print instead and print your own spaces between the node contents.
 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:The new-lines happen automatically as part of System.out.println. So if you don't want them, use System.out.print instead and print your own spaces between the node contents.


That's what I did, but after the last element, there should be no space, so the system gets the exact answer... I did this to avoid an error from the checking system of my faculty:




But is there any other way of doing this?
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bashkim Kaspersky wrote:That's what I did, but after the last element, there should be no space, so the system gets the exact answer... I did this to avoid an error from the checking system of my faculty:
But is there any other way of doing this?

You could avoid repeating the call to getElement. This makes it look a bit tidier, but I don't think there's room for much more improvement.


Or you could build your complete output in a StringBuilder and then delete the final space before printing it, but obviously this is going to use more memory.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bashkim Kaspersky wrote:... after the last element, there should be no space...


Then turn things around, and put a space before all elements except the first.
 
Kaspersky Ukshini
Ranch Hand
Posts: 122
C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll just go with the curent solution, nevermind.
Thank you soooo much for walking me through the problem... Getting more and more and morep repared for the exam
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!