楼主没看到过Java与C的一个区别就是去除了令人头疼的指针吗?

解决方案 »

  1.   

    没有指针就不行了吗?
    basic以前也不照样写吗?
    指针灵活但难理解
    提供指针只是多一种手段
    并不是说没有指针就不型了
    看来楼主很依赖指针
      

  2.   

    class stack
    {
    int stck[]=new int[10];
    int tos;

    stack()
    {
    tos=-1;
    }
    void push(int item)
    {
    if(tos==9)
    {
    System.out.println("Stack is full!");
    }else{
    stck[++tos]=item;
    }
    }

    int pop()
    {
    if(tos<0)
    {
    System.out.println("Stack is underFlow!");
    return 0;
    }else{
    return stck[tos--];
    }
    }
    }
    class testStack
    {
    public static void  main(String[] args)
    {
    stack mystack1=new stack();
    stack mystack2=new stack();

    for(int i=0;i<10;i++)  mystack1.push(i);
    for(int i=10;i<20;i++) mystack2.push(i);

    System.out.println("Stack in mystack1:");
    for(int i=0;i<10;i++)
    {
    System.out.println(mystack1.pop());
    }

    System.out.println("Stack in mystack2:");
    for(int i=0;i<10;i++)
    {
    System.out.println(mystack2.pop());
    }
    }
    }
      

  3.   

    java.util.LinkedList
    LinkedList类是封装好的链表结构
    其他的可以看一下java库中关于集合的一些类
      

  4.   

    提出如此弱智的问题的人论坛上也是头一次见到。数据结果和算法一定要显式地使用指针才能实现吗?
     此问题相当于“汽车没有腿,如何移动?”。CSDN第一大蠢贴。别在这里丢人现眼啦...
      

  5.   

    好好理解一下DS吧^_^,下面是LinkedList,上学期学DS的时候写的
    //ADT of list
    interface List{
    public void clear();
    public void insert(Object item);
    public void append(Object item);
    public Object remove();
    public void setFirst();
    public void next();
    public void prev();
    public int length();
    public void setPos(int pso);
    public void setValue(Object val);
    public Object currValue();
    public boolean isInList();
    public void print();
    }
    class Link{//节点类
    private Object element;
    private Link next; Link(Object it,Link nextval){
    element  = it;
    next = nextval;
    }
    Link(Link nextval){
    next = nextval;
    }
    Link next() {return next;}
    Link setNext(Link nextval) {return next = nextval;}
    Object element(){return element;}
    Object setElement(Object it){ return element = it;}
    }
    class LList implements List{//单链表
    private Link head;
    private Link tail;
    protected Link curr; LList(int sz){setup();}
        LList(){setup();} private void setup(){
    tail = head = curr = new Link(null);
    } public void clear(){
    head.setNext(null);
    curr = tail = head;
    } public void insert(Object it){
    Assert.notNull(curr,"No current element");
    curr.setNext(new Link(it,curr.next()));
    if(tail == curr)
    tail = curr.next();
    } public void append(Object it){
    tail.setNext(new Link(it,null));
    tail = tail.next();
    }

    public Object remove(){
    if(! isInList()) return null;
    Object it = curr.next().element();
    if(tail == curr.next()) tail = curr;
    curr.setNext(curr.next().next());
    return it;
    } public void setFirst(){
    curr = head;
    } public void next(){
    if(curr != null) 
    curr = curr.next();
    }

    public void prev(){
    if((curr == null) || (curr == head))
    {curr = null;return;}
    Link temp = head;
    while((temp != null) && (temp.next() != curr))
    temp = temp.next();
    curr = temp;
    } public int length(){
    int cnt = 0;
    for(Link temp = head.next();temp != null;temp = temp.next())
    cnt++;
    return cnt;
    } public void setPos(int pos){
    curr = head;
    for(int i = 0;(curr != null) && (i < pos);i++)
    curr = curr.next();
    } public void setValue(Object it){
    Assert.notFalse(isInList(),"Not in List");
    curr.next().setElement(it);
    } public Object currValue(){
    if(! isInList()) return null;
    return curr.next().element();
    } public boolean isEmpty(){
    return head.next() == null;
    } public boolean isInList(){
    return (curr != null) && (curr.next() != null);
    } public void print(){
    if(isEmpty()) System.out.println("()");
    else{
    System.out.print("(");
    for(setFirst();isInList();next())
    System.out.print(currValue() + " ");
    System.out.println(")");
    }
    }
    }
      

  6.   

    你也懂什么叫系统开支吗?少来卖弄名词,你大概以为卖弄数据结构,算法等等几个次就能跻身高手的行列吧?
    懒得回答你。 star821116(原来爱曾给我美丽心情)给出的是标准包里List的实现,不是叫你照着打一遍。不过鉴于你也不会懂什么叫封装,我也不多说了。
        反正看到你这个SB的帖子就来气!你以后出来一次我就骂你一次。
      

  7.   

    java的类库包含了很多有用的类和函数,可以很方便的实现C和C++用指针实现的功能,而且更安全,就是效率稍低!!!不过很好用!!!
      

  8.   

    java不需要指针,引用一句话:“万事万物皆对象”
      

  9.   

    建议 :stillwater123 (南楠) 去看看JAVA与模式的腌菜与回鲜。
      

  10.   

    针对最长的那个程序,其中的Link类性是不是库自带的?我看了《数据结构(java版)》,里面的链表是开一个静态数组,next域其实是数组元素的角码(int型),这算什么动态链表啊?
      

  11.   

    你又出现啦?你不是4天学会java么?怎么过了几个月,我都从1三角升到4三角了,你还是跟个菜鸡没什么区别啊?
      

  12.   

    楼主真的理解数据结构了吗?
    链表是相对于线性表而言的
    当然可以用数组来实现
    不仅数据结构java版中有
    你去看看C、C++版都有先好好看看数据结构吧
      

  13.   

    另外说到系统开销这个问题本身就说明楼主对Java不是很了解说到性能Java的性能确实不如C/C++
    说到性能,汇编性能最好,是不是也要拿Java和汇编比一比啊?可笑可是Java实现了跨平台,别说汇编了,C/C++也不行你这是拿着Java天生缺憾在找事儿,与数据结构一点儿关系都没有
      

  14.   

    楼主应该是对c比较了解,但你学了c++吗,里面有引用,java和c++很象,但也有区别,比如java不支持多重继承,但它又有接口,好好看看基础语法书吧
      

  15.   

    4 天学完java的帖子我今天下午看了一下午啊。
    我喜欢楼主的坚韧
      

  16.   

    粘一个我在学的书上(《Java How to program》)的例子。
    // Fig. 22.3: List.java
    // Class ListNode and class List definitions
    package com.deitel.jhtp3.ch22;class ListNode {
       // package access data so class List can access it directly
       Object data;    
       ListNode next;   // Constructor: Create a ListNode that refers to Object o.
       ListNode( Object o ) { this( o, null ); }   // Constructor: Create a ListNode that refers to Object o and
       // to the next ListNode in the List.
       ListNode( Object o, ListNode nextNode )
       {
          data = o;         // this node refers to Object o
          next = nextNode;  // set next to refer to next
       }   // Return a reference to the Object in this node
       Object getObject() { return data; }   // Return the next node
       ListNode getNext() { return next; }
    }// Class List definition
    public class List {
       private ListNode firstNode;
       private ListNode lastNode;
       private String name;  // String like "list" used in printing   // Constructor: Construct an empty List with s as the name
       public List( String s )
       {
          name = s;
          firstNode = lastNode = null;
       }   // Constructor: Construct an empty List with
       // "list" as the name
       public List() { this( "list" ); }                                // Insert an Object at the front of the List
       // If List is empty, firstNode and lastNode will refer to
       // the same object. Otherwise, firstNode refers to new node.
       public synchronized void insertAtFront( Object insertItem )
       {
          if ( isEmpty() )
             firstNode = lastNode = new ListNode( insertItem );
          else 
             firstNode = new ListNode( insertItem, firstNode );
       }   // Insert an Object at the end of the List
       // If List is empty, firstNode and lastNode will refer to
       // the same Object. Otherwise, lastNode's next instance
       // variable refers to new node.
       public synchronized void insertAtBack( Object insertItem )
       {
          if ( isEmpty() )
             firstNode = lastNode = new ListNode( insertItem );
          else 
             lastNode = lastNode.next = new ListNode( insertItem );
       }   // Remove the first node from the List.
       public synchronized Object removeFromFront()
              throws EmptyListException
       {
          Object removeItem = null;      if ( isEmpty() )
             throw new EmptyListException( name );      removeItem = firstNode.data;  // retrieve the data      // reset the firstNode and lastNode references
          if ( firstNode.equals( lastNode ) )
             firstNode = lastNode = null;
          else
             firstNode = firstNode.next;      return removeItem;  
       }   // Remove the last node from the List.
       public synchronized Object removeFromBack()
              throws EmptyListException
       {
          Object removeItem = null;      if ( isEmpty() )
             throw new EmptyListException( name );      removeItem = lastNode.data;  // retrieve the data      // reset the firstNode and lastNode references
          if ( firstNode.equals( lastNode ) )
             firstNode = lastNode = null;
          else {
             ListNode current = firstNode;         while ( current.next != lastNode )  // not last node
                current = current.next;      // move to next node
       
             lastNode = current;
             current.next = null;
          }      return removeItem;
       }   // Return true if the List is empty
       public synchronized boolean isEmpty()
          { return firstNode == null; }   // Output the List contents
       public synchronized void print()
       {
          if ( isEmpty() ) {
             System.out.println( "Empty " + name );
             return;
          }      System.out.print( "The " + name + " is: " );      ListNode current = firstNode;      while ( current != null ) {
             System.out.print( current.data.toString() + " " );
             current = current.next;
          }      System.out.println( "\n" );
       }
    }/**************************************************************************
     * (C) Copyright 1999 by Deitel & Associates, Inc. and Prentice Hall.     *
     * All Rights Reserved.                                                   *
     *                                                                        *
     * DISCLAIMER: The authors and publisher of this book have used their     *
     * best efforts in preparing the book. These efforts include the          *
     * development, research, and testing of the theories and programs        *
     * to determine their effectiveness. The authors and publisher make       *
     * no warranty of any kind, expressed or implied, with regard to these    *
     * programs or to the documentation contained in these books. The authors *
     * and publisher shall not be liable in any event for incidental or       *
     * consequential damages in connection with, or arising out of, the       *
     * furnishing, performance, or use of these programs.                     *
     *************************************************************************/
      

  17.   

    虚心请教问题?
    真不像http://expert.csdn.net/Expert/topic/2629/2629891.xml?temp=.1389582java完了是Basic,我倒觉得他只是在挖坑
    至于我们灌的水,他估计一点儿都不看
      

  18.   

    其实JAVA的引用和C++的指针是很象的,只是表达起来更方便了,甚至可以说是指针的简单表达,数据结构是可以用数组模拟的,有空看看<<c++ primer>>和JAVA版的数据结构你就很明白了.
      

  19.   

    楼主,搞笑吧??还穿开裆裤吧?Naive啊!
      

  20.   

    建议楼主看think in java,如果你是自学,并没有任务要赶,不需要有个问题就问,现将问题记着,再继续看书了,将看得懂的,真正看懂了,那些看不懂的也就快懂了
    有问题就问不是好的学习方法
      

  21.   

    《数据结构(java版)》你看过吗?它开了两个数组,data[20],next[20],data[i]表示第i个结点的数据域,next[i]表示第i个结点的next指针域,它的值是data的角码,这种方式不是静态的是什么??我不是说鄙弃静态,可是在做运筹学里的算法、游戏里的人工智能、图形学里的矢量化、等等领域时,静态确实力不从心。lion168(老狼),你说“其实JAVA的引用和C++的指针是很象的,只是表达起来更方便了,甚至可以说是指针的简单表达”,就是说能够动态实现了,能不能举一个简单的例子啊,不要长,我菜!
      

  22.   

    我来说两句先说技术问题:
    实现数据结果和算法不一定要用指针,在java中,我们用“句柄”
    这就相当于指针。但比指针好,因为有类型检查。灌水:
    对于楼主,一开始我还以为楼主做了名人后变谦虚了,也想附和一下叫大家不要再攻击他
    不过看了
    http://expert.csdn.net/Expert/topic/2629/2629891.xml?temp=.1389582
    觉得呀,唉...“30分的大帖竟然没人回?是没人会?”江山依旧在,几度夕阳红啊。
      

  23.   

    GFox(小狐)555你怎么对哀家怎么了解啊!莫非就是传说中的知己??shart(浪客剑心)哀家算什么名人啊,偶只是想向大家虚虚心心的虚虚心心的求教恳求乞求奢求讨求一点点基础的问题,然后在大家的关怀和指导下做一个KOF的剧情回合版,师姨长技以治夷,意图为振兴我国游戏软件事业 出千分力 放万点光
      

  24.   

    《数据结构(java版)》你看过吗?它开了两个数组,data[20],next[20],data[i]表示第i个结点的数据域,next[i]表示第i个结点的next指针域,它的值是data的角码,这种方式不是静态的是什么??我不是说鄙弃静态,可是在做运筹学里的算法、游戏里的人工智能、图形学里的矢量化、等等领域时,静态确实力不从心。----------------------------------------------------------------------
    书上用静态数组举个例子,你就只会用静态数组了?FT死了,当时我面试的时候还没有想过Java怎么实现数据结构呢,面试的人就让我想一下用Java怎么实现树结构还不是二叉树,照你这么说,我直接回答书上没教算了楼主不是一直吹嘘有思想吗?
    http://expert.csdn.net/Expert/topic/2276/2276584.xml?temp=.8083155怎么这会儿就?汗 -_-b
      

  25.   

    哀家算什么名人啊,偶只是想向大家虚虚心心的虚虚心心的求教恳求乞求奢求讨求一点点基础的问题,然后在大家的关怀和指导下做一个KOF的剧情回合版,师姨长技以治夷,意图为振兴我国游戏软件事业 出千分力 放万点光----------------------------------------------------------------------这样的帖子不回也罢了,吐个隔夜饭先,恶... -_-||||||||||
      

  26.   

    唉,别跟他们闷气,谁是生下来就什么都知道啊,还不是慢慢问慢慢学,不耻下问
    还有错啊,这不是有这么多人给答案了么,在这里就要学会厚脸皮,goodluck!!