|
well i have heard it first time ('kiosk technique') so i will look into that.......
|
|
|
|
|
import java.io.*;
import java.util.*;
class LinkedList<T>
{
private Node head;
private Node last;
private T data;
public LinkedList()
{
head = null;
}
public boolean isEmpty()
{
return (head==null);
}
public void addToHead (T val)
{
Node n = new Node (val);
n.next = head;
head = n;
}
private class Node
{
public T data;
public Node next;
Node (T val)
{
data = val;
next = null;
}
}
public void delete(T val)
{
delete(head, val);
}
public Node delete (Node curr, T val)
{
Comparable <T> temp = (Comparable<T>)val;
if (curr==null)
{
System.out.println("list empty");
}else if ( temp.compareTo(curr.data)==0){
curr = curr.next;
}else{
curr.next = delete(curr.next, val);
}
return curr;
}
public String toString()
{
String str ="";
Node curr = head;
while(curr!=null)
{
str = str + curr.data + " ";
curr = curr.next;
}
return str + "\n";
}
}
class Link
{
public static void main(String[] arg)
{
LinkedList<Integer> s1 = new LinkedList<Integer>();
s1.addToHead(23);
s1.addToHead(78);
s1.addToHead(100);
s1.delete(23);
System.out.println(s1.toString());
}
}
I am writing the code above to delete a node from a linkedlist using recursion.
Questions
1. Is there another way I can rewrite the method without using Comparable?
2. The program compiles but give the following error all the time, How can i rectify this?
Note: C:\Users\antonio\Desktop\Link2.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
|
|
|
|
|
T is an Object and supports equals(Object) . Use that instead of a comparitor which is more suitable for ordered items. If the item T will always be comparible then you can define it as such:
class LinkedList<T extends Comparable<T>>
Panic, Chaos, Destruction.
My work here is done.
or "Drink. Get drunk. Fall over." - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
|
|
|
|
|
As regards your second question, if you follow the compiler's advice and recompile with -Xlint:unchecked, you get the following warning:
warning: [unchecked] unchecked cast
found : T
required: java.lang.Comparable<T>
Comparable<T> temp = (Comparable<T>) val;
1 warning
In other words, there is no guarantee that the val being passed into the delete method will actually be a Comparable at runtime, so the cast may fail. This isn't a compilation error, because it will work if you do pass in a Comparable, but there is no way for the compiler to enforce this, so it is letting you know that this may be unsafe.
And as someone else has already pointed out, it's not clear why you are using compareTo instead of equals.
|
|
|
|
|
Dear programmers,
I am a new student of CS, I am learning Java on Eclipse. I have an assignment which to reverse an input text - it is called palindrome. I coded but errors, I wonder if you all can help me as I do not even know that what wrong with this code.
public class firstsubroutine {
public static void main (String[] args){
TextIO.putln("Enter the palindrome : ");
TextIO.put("? ");
String line = TextIO.getln();
line = line.toLowerCase();
if (isString(line) = true){
reverse(line);
String reversed = reverse(line);
if (line == reversed){
System.out.println ("Stripped : " + line);
System.out.println ("Reversed : " + reversed);
System.out.println ("This IS a palindrome.");
}
else{
System.out.println ("Stripped : " + line);
System.out.println ("Reversed : " + reversed);
System.out.println ("This is NOT a palindrome.");
}
}
}
static void isString (String isStr){
int i;
boolean isBoolean;
for (i = 0; i < isStr.length(); i++){
char ch = isStr.charAt(i);
if (Character.isLetter(ch)){
isBoolean = true;
}
else{
isBoolean = false;
}
}
}
static void reverse(String reStr){
int i;
String reverse = "";
for (i = reStr.length() - 1; i >= 0; i++){
reverse = reverse + reStr.charAt(i);
}
}
}
|
|
|
|
|
I found few mistakes. I've commented them and your program is working fine..
import java.util.Scanner;
public class firstsubroutine {
public static void main (String[] args){
Scanner in = new Scanner(System.in);
System.out.println("Enter the palindrome : ");
System.out.println("? ");
String line = in.next();
line = line.toLowerCase();
if (isString(line) == true){
String reversed = reverse(line);
if (line.equals(reversed)){
System.out.println ("Stripped : " + line);
System.out.println ("Reversed : " + reversed);
System.out.println ("This IS a palindrome.");
}
else{
System.out.println ("Stripped : " + line);
System.out.println ("Reversed : " + reversed);
System.out.println ("This is NOT a palindrome.");
}
}
}
static boolean isString (String isStr){
int i;
for (i = 0; i < isStr.length(); i++){
char ch = isStr.charAt(i);
if (!Character.isLetter(ch))
{
return false;
}
}
return true;
}
static String reverse(String reStr){
int i;
String reverse = "";
for (i = reStr.length() - 1; i >= 0; i--){
reverse = reverse + reStr.charAt(i);
}
return reverse;
}
}
Shivam Kalra
|
|
|
|
|
Shivam Kalra,
Many and many thanks for you support. I tested and worked very well.
Visoth
|
|
|
|
|
No problem bro.
I'm a CS student too. And homework sucks.
|
|
|
|
|
Oh, my friend!!!! I wish I could help you. What language do you use for your homework?
Visoth
|
|
|
|
|
We are using combination of C++ and JAVA.
|
|
|
|
|
I know nothing about C++ but Java is learning.
Anyways, where are you from? I'm Visoth, from Cambodia. Nice to know you here.
I really doubt if you can give me your email address or IM.
Visoth
|
|
|
|
|
|
see - it works... shows code - gets comments - resolved!!
@ Visoth
welcome. Well asked, I appreciate that. Feel free to ask more!
regards
Torsten
I never finish anyth...
|
|
|
|
|
Well, thanks. Are you a programmer?
Visoth
|
|
|
|
|
No - I'm a developer.
To my point of view there is a certain difference between programming and developing.
Programming is just a code based implementation of what is wanted. To me, there has been something planed by another person and one is just responsible for the code.
Developing means to make the whole thing by myself. I'm writing the system architecture (for my parts, some are discuss in the team), the documentation and all other stuff that's needed throughout the development process.
regards
Torsten
I never finish anyth...
|
|
|
|
|
Hi,
From this site[^], where is the JRE in the Notes installation folder? I've installed Lotus Notes 8.5
|
|
|
|
|
It means the folder where the JRE is installed, below the folder where you installed Lotus Notes. A quick view with Windows Explorer will find it for you.
I must get a clever new signature for 2011.
|
|
|
|
|
Under my "C:\Program Files\IBM\Lotus\Notes", there are folders
1.Data
2.framework
3.icc
4.jvm
5.license
6.MUI
7.ndext
8.properties
9.xmlschemas
10.xsp
Tried windows search, but there's no folder name with "JRE" under my Notes installation folder.
|
|
|
|
|
I guess it may be in the jvm subdirectory, you will need to check the content to see if it exists there. Alternatively go back to the site where you downloaded this and ask there.
I must get a clever new signature for 2011.
|
|
|
|
|
I have a Java application which was running on the console, but now this application will be called by a Utility. Is there any way by which the my application returns some value which the utility can understand ?
For eg if its 1: then successful
2:Failed
3:File Not found.
|
|
|
|
|
There is a difference between whether it could and whether it does.
The normal exit value that would indicate success from an application would be zero.
And value except that depends on the application.
An application can fail totally and still return zero unless it is specifically programmed to do differently.
If and only if the application uses the System.exit() method then it can return other values.
Other than that command line tools often use stdio and sometimes stderr. The contents of that would need to be interpreted by any containing application.
|
|
|
|
|
Thanks a lot for your answer.
Can you give me some details on stdio and stderr,like how are they used
|
|
|
|
|
|
hi
where i can find one telnet client source code for interactive with any telnet server ?
thanks
|
|
|
|
|
navidnmc wrote: where i can find one telnet client source code for interactive with any telnet server ?
All over the internet; a quick Google will yield many samples.
I must get a clever new signature for 2011.
|
|
|
|