佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1063|回复: 9

Java Linked List 问题 (已解决)

[复制链接]
发表于 27-7-2009 07:31 PM | 显示全部楼层 |阅读模式
嘿各位好,有Java的问题想请教,以下是我的code :
import java.util.*;
import java.io.*;
import java.lang.*;

public class StackAss2
{
  public static void main (String args[])
  {         
    int values;
    String numbers;
    LinkedList in = new LinkedList();
    LinkedList temp = new LinkedList();
    InputStreamReader input = new InputStreamReader(System.in);
    BufferedReader reader = new BufferedReader(input);

    try
    {
      for (int a=0; a<5; a++)
      {
        System.out.print("enter " + a + " number : " );
        numbers = reader.readLine();

        values = Integer.parseInt(numbers);
        in.add(values);
      }
    }

    catch(IOException ioe)
    {
      System.out.println("Error!" );
      System.exit(1);
    }
        System.out.println(in);
        /*System.out.println(in.get(1));
        System.out.println(in.get(2));
        System.out.println(in.get(3));
        System.out.println(in.get(4));
        System.out.println(in.get(5));*/
   

        int maxx;
        int waha;
        for (int i=0; i<5; i++)
          {
                  maxx = 0;
                  //问题在这里!!!!
                  waha == in.get(i);
          if (waha > maxx)
            {
                   temp.add(waha);
            }
        System.out.println(maxx);
    }
   //。。。。。。。。
  }
}

我的目的是要用在linked list找出max value。
但compile了后,一直出现 "not a statement"这个message。
如果把 waha == in.get(i); 换成 waha = in.get(i);
就会出现 "incompatible types" error message.
要怎样把那个 in.get(i) 的 value 变成 waha 呢?
先谢谢各位指点咯  

[ 本帖最后由 nick_khor 于 5-8-2009 12:46 PM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

发表于 27-7-2009 07:45 PM | 显示全部楼层
int maxx;
        int waha;
        for (int i=0; i<5; i++)
          {
                  maxx = 0;
                  //问题在这里!!!!
                  waha == in.get(i);

          if (waha > maxx)
            {
                   temp.add(waha);
            }
        System.out.println(maxx);
    }


我个人觉得原因是因为你的waha没有把它等于一个号码
因此当你要compare的时候就会变成
??>maxx
因为电脑不知道你的waha是什么
如果错了不要骂我哦~~~
回复

使用道具 举报

发表于 27-7-2009 08:04 PM | 显示全部楼层
因为in.get(1) return 的是object type.
要assign给integer variable之前, 需要explicit convert to integer.

waha = (int)int.get(1);
回复

使用道具 举报

发表于 27-7-2009 08:10 PM | 显示全部楼层
原帖由 falconsoon 于 27-7-2009 08:04 PM 发表
因为in.get(1) return 的是object type.
要assign给integer variable之前, 需要explicit convert to integer.

waha = (int)int.get(1);


对ho~~~我真的是笨蛋没有看到这个问题~~~~
回复

使用道具 举报

发表于 27-7-2009 08:10 PM | 显示全部楼层
原帖由 nick_khor 于 27-7-2009 07:31 PM 发表
嘿各位好,有Java的问题想请教,以下是我的code :

我的目的是要用在linked list找出max value。
但compile了后,一直出现 "not a statement"这个message。
如果把 waha == in.get(i); 换成 waha = in.get(i);
...


waha == in.get(i) 是Comparisom, 而不是正确的Statement
应该用assignment operator (=)
in.get(i)会return Object Type (参考 http://java.sun.com/j2se/1.4.2/d ... LinkedList.html#get(int))
所以你需要Casting,因为waha的data type 是int,而in.get(i)所return的data type是 Object
waha = (int)in.get(i);

几年没写过Java了
如果错了,请其他高手指点

[ 本帖最后由 sfkwan 于 27-7-2009 08:13 PM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 27-7-2009 08:38 PM | 显示全部楼层
hm.谢谢各位
改了
waha = (int)in.get(i);

反而出现 inconvertible types;
从来没看过这个error message, googling~
回复

使用道具 举报

Follow Us
发表于 27-7-2009 09:20 PM | 显示全部楼层

回复 6# nick_khor 的帖子

waha = (int)in.get(i);

换取

waha = (Integer)in.get(i);
回复

使用道具 举报

 楼主| 发表于 27-7-2009 09:20 PM | 显示全部楼层
哦.. 谢谢各位
问题搞定~

    LinkedList<Integer>in = new LinkedList<Integer>();
    LinkedList<Integer>temp = new LinkedList<Integer>();

过后这个statement是对的
waha = in.get(i);
回复

使用道具 举报


ADVERTISEMENT

发表于 27-7-2009 09:23 PM | 显示全部楼层
如果没加入 <Integer> 也就是 generic , 类是 c++ 的 template

in.get(i); 的 return type 是 object。

Object 是说有class 的super class。 但是 int, double 等是 non permissive class, 也就是他们不属于任何的 class, 因此, Object 如法转换为 int。

因此 java 才有了 Integer, Double 等class, 让他们成为Object, 方便转换
回复

使用道具 举报

 楼主| 发表于 27-7-2009 09:25 PM | 显示全部楼层
原帖由 onlylonly 于 27-7-2009 09:23 PM 发表
如果没加入  也就是 generic , 类是 c++ 的 template

in.get(i); 的 return type 是 object。

Object 是说有class 的super class。 但是 int, double 等是 non permissive class, 也就是他们不属于任何的 cl ...

原来如此,非常谢谢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 6-12-2025 03:36 PM , Processed in 0.152460 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表