Results 1 to 5 of 5

Thread: Return value of generic objects in java

  1. #1
    Join Date
    Mar 2010
    Posts
    162

    Return value of generic objects in java

    Hello,
    I am a beginner in java programming language. I started making a list and then interface with I implement an ArrayList then the signature should be to retrieve the item on its index.
    Here is my code:
    Code:
    package bat;
    
    import java.util.ArrayList;
     
    Public class TabArray <T> implements <T> List {
        private ArrayList ele;
    
    
        Public T item(int idx){
            return this.ele.get(idx);
        }
    Any idea on how to do this. Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    356

    Re: Return value of generic objects in java

    Hello,
    You make generic, but you declare your ArrayList as in 1.4, that is the reason why it is not working in your code.
    Here is what I am talking about
    Code:
    ArrayList ele <T>
    If you correct this in your code then your code will surely work. Try replacing this with the correct syntax and then try to compile your program.

  3. #3
    Join Date
    Nov 2009
    Posts
    330

    Re: Return value of generic objects in java

    Hey
    I have modified your code, see if this works now. If not then post back with the error you are getting and we will try to sort your problem.
    Code:
    package battery;
    
    Public class Table <T> implements <T> List {
        private T[] ele;
        private int temp;
    
        Public Board(int ably) {
           this.ele = new T[ably];
           this.temp=0;
        }

  4. #4
    Join Date
    Nov 2009
    Posts
    446

    Re: Return value of generic objects in java

    Hello,
    You can create tables perfectly generic.
    You can take the example of Vector <T>
    Here is the sample code for the same
    Code:
    Public class Vector <T> {
        Public T tes;
    }
    This is not the complete code, but just the part of the code so that you can get an idea about how you have to proceed with the coding part. I hope this will help you and you will find a way round.

  5. #5
    Join Date
    Nov 2009
    Posts
    356

    Re: Return value of generic objects in java

    Hello,
    You have take the reference of the sample code such as
    Code:
    Public class Test <T> {
      Public void create(){
        T[] t;
        t = new T[50];
      }
    }
    If you are going to run this program, then the conclusion is that
    Code:
    Can not create a generic array of T
    I hope you are understanding the thing correctly here.

Similar Threads

  1. Advanced Cache Objects in Java
    By StudyBoy in forum Tips & Tweaks
    Replies: 3
    Last Post: 31-07-2010, 03:41 PM
  2. Implement the return type in java
    By Captain Carrot in forum Software Development
    Replies: 4
    Last Post: 25-03-2010, 01:34 PM
  3. Array of objects in java
    By Ash maker in forum Software Development
    Replies: 5
    Last Post: 27-02-2010, 02:05 AM
  4. What are an Immutable Objects in Java?
    By Silent~Kid in forum Software Development
    Replies: 4
    Last Post: 19-02-2010, 06:35 AM
  5. Access to Objects In A JavaBean From Another Java Bean
    By ramsun in forum Software Development
    Replies: 5
    Last Post: 30-01-2010, 01:18 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,701,791.06515 seconds with 16 queries