Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,283 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,378 people online right now. Registration is fast and FREE... Join Now!




Lab5 Data Structure

 
Reply to this topicStart new topic

Lab5 Data Structure, Generics Problem

redzuan
2 Sep, 2008 - 06:27 AM
Post #1

New D.I.C Head
*

Joined: 21 Jul, 2008
Posts: 47


My Contributions
CODE
public class MyArray <T extends Comparable<? super T>>
{
    
    
    private T[] arr;
    
    
    
    public MyArray(T[] a)
    {
     arr = Arrays.copyOf(a, a.length);  
    }


   public T max()
    {
        //int size = arr.length;
        
        T maximum = arr[0];
        
        for(int i=1;i<arr.length;i++)
        {
            if(arr[i].compareTo(maximum)>0)
            maximum=arr[i];
            
        }
        return maximum;
      
    }
}



CODE

public class Main {

    public static void main(String[] args) {
        
      
        
        Integer[] intArr ={new Integer(20), new Integer(30), new Integer(10), new
                Integer(80), new Integer(70), new Integer(100)};
      
        
         MyArray<Integer> myInt = new MyArray<Integer>(intArr);
        
        
        
        System.out.println("Tnteger array:");
        for(int i=0;i<intArr.length;i++)
        {
            System.out.print(intArr[i]+" ");
        }
        System.out.println("Max:"+ myInt.max());
    }

}


here is my problem:

MyArray<Integer> myInt = new MyArray<Integer>(intArr)

it said that the parameter is not within its bound
can anyone help me what is about....
User is offlineProfile CardPM
+Quote Post

JeroenFM
RE: Lab5 Data Structure
2 Sep, 2008 - 11:15 AM
Post #2

D.I.C Head
Group Icon

Joined: 30 Jun, 2008
Posts: 186



Thanked: 9 times
Dream Kudos: 100
My Contributions
QUOTE(redzuan @ 2 Sep, 2008 - 07:27 AM) *

CODE
public class MyArray <T extends Comparable<? super T>>
{
    
    
    private T[] arr;
    
    
    
    public MyArray(T[] a)
    {
     arr = Arrays.copyOf(a, a.length);  
    }


   public T max()
    {
        //int size = arr.length;
        
        T maximum = arr[0];
        
        for(int i=1;i<arr.length;i++)
        {
            if(arr[i].compareTo(maximum)>0)
            maximum=arr[i];
            
        }
        return maximum;
      
    }
}



CODE

public class Main {

    public static void main(String[] args) {
        
      
        
        Integer[] intArr ={new Integer(20), new Integer(30), new Integer(10), new
                Integer(80), new Integer(70), new Integer(100)};
      
        
         MyArray<Integer> myInt = new MyArray<Integer>(intArr);
        
        
        
        System.out.println("Tnteger array:");
        for(int i=0;i<intArr.length;i++)
        {
            System.out.print(intArr[i]+" ");
        }
        System.out.println("Max:"+ myInt.max());
    }

}


here is my problem:

MyArray<Integer> myInt = new MyArray<Integer>(intArr)

it said that the parameter is not within its bound
can anyone help me what is about....


Works fine on my computer, could you be a bit more specific about the error?

CODE

Tnteger array:
20 30 10 80 70 100 Max:100

User is offlineProfile CardPM
+Quote Post

redzuan
RE: Lab5 Data Structure
3 Sep, 2008 - 01:47 AM
Post #3

New D.I.C Head
*

Joined: 21 Jul, 2008
Posts: 47


My Contributions
thanks for ur afford n time, i already solve my progrm
CODE



public class Main {

    
    public static void main(String[] args) {
        Integer[] intArr ={new Integer(20), new Integer(30), new Integer(10), new
                Integer(80), new Integer(70), new Integer(100)};
        String[] strArr ={ "red", "blue", "green", "black", "brown", "yellow"};
        
         MyArray<Integer> myInt = new MyArray<Integer>(intArr);
         MyArray<String> myStr = new MyArray(strArr);
        
        
        System.out.println("Integer array:");
        for(int i=0;i<intArr.length;i++)
        {
            System.out.print(intArr[i]+" ");
        }
        System.out.println("\nMax:"+ myInt.max());
        
        myInt.shuffle();
        System.out.println("Shuffling...\n"+myInt.toString());
        
        myInt.shuffle();
        System.out.println("Shuffling again...\n"+myInt.toString());
        
        myInt.shuffle();
        System.out.println("Shuffling one more time...\n"+myInt.toString());
        
        System.out.println("String array:");
        for(int i=0;i<strArr.length;i++)
        {
            System.out.print(strArr[i]+" ");
        }
        System.out.println("\nMax:"+ myStr.max());
        
        myStr.shuffle();
        System.out.println("Shuffling...\n"+ myStr.toString());
        
        myStr.shuffle();
        System.out.println("Shuffling again...\n"+ myStr.toString());
        
        myStr.shuffle();
        System.out.println("Shuffling one more time...\n"+ myStr.toString());
        
        
        
        
    }

    

}

import java.util.Arrays;
import java.util.Random;


public class MyArray <T extends Comparable<? super T>>
{
    
    
     T[] arr;
    
    
    
    public MyArray(T[] a)
    {
     arr = Arrays.copyOf(a, a.length);  
    }


   public T max()
    {
        
        
        T maximum = arr[0];
        
        for(int i=1;i<arr.length;i++)
        {
            if(arr[i].compareTo(maximum)>0)
            maximum=arr[i];
            
        }
        return maximum;
      
    }
   public void shuffle()
    {
        int i;
        T temp;
        Random rgen = new Random();
      
             for( i=arr.length-1;i>1;i--)
             {
                 int randomPosition = rgen.nextInt(i);
                  temp = arr[randomPosition];
                 arr[randomPosition] = arr[i];
                 arr[i]=temp;
                
                
             }
        
      
        
   }  
  
   public String toString()
   {
         String temp = "";
        
         for (int i = arr.length-1; i>=0; i--){
            temp = temp + " " + arr[i] + " ";
        }
        
         return temp;
      
   }
}

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 05:33AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month