Results 1 to 2 of 2
  1. #1
    BookOfEliX is offline Member
    Join Date
    Feb 2015
    Posts
    20
    Rep Power
    0

    Default int cannot be dereferenced.

    As the title states, that is the error code that I'm getting. It's in line 148. I know compareTo is used for objects. What would I use for a int element in the array?

    Java Code:
    import java.util.*;
    public class test{
       public static void main(String[] args){
          int firstSort =(int)(Math.random() * 100);
          int secondSort = (int)(Math.random() * 100);
          int thirdSort = (int)(Math.random() * 100);
          int[] a = new int[1000000];
          int[] b = new int[1000000];
          int[] c = new int[1000000];
          
          for(int i = 1; i <= 1000000; ++i){
            a[i-1] = i;
            //System.out.println(a[i-1]);
          }
          
          for(int i = 1000000 ; i >= 1; i-- ){
            b[i-1] = i;
            //System.out.println(b[i-1]);
          }
          for(int i = 1; i <= 100; ++i){
             c[i-1] = (int)(Math.random() * 1000000);
             //System.out.println(c[i-1]);
          }
          
          //This will check the time for the selection sort method (increasing)
          /*long startTime = System.nanoTime();
          sort(a);
          long endTime= System.nanoTime() - startTime; 
          System.out.println("The running time for the selection sort in increasing order is  + " + endTime/1000 + " Nanoseconds.");*/
          
          //This will check the time for the selection sort method (decreasing)
    /*      long startTime1 = System.nanoTime();
          sort(b);
          long endTime1= System.nanoTime() - startTime1; 
          System.out.println("The running time for the selection sort in decreasing order is + " + endTime1/1000 + " Nanoseconds.");*/
          
          //This will check the time for the selection sort method (random)
         /* long startTime2 = System.nanoTime();
          sort(b);
          long endTime2= System.nanoTime() - startTime2; 
          System.out.println("The running time for the selection sort in decreasing order is + " + endTime2/1000 + " Nanoseconds.");*/
          //This will check the time time for quick sort method (increasing) 
          long startTime3 = System.nanoTime();
          partition(a, 0, a.length-1);
          long endTime3= System.nanoTime() - startTime3; 
          System.out.println("The running time for the Quicksort in increasing order is  + " + endTime3/1000 + " Nanoseconds.");
          //This will check the time for quick sort method (decreasing) 
          long startTime4 = System.nanoTime();
          partition(a, 0, a.length-1);
          long endTime4= System.nanoTime() - startTime4; 
          System.out.println("The running time for the Quicksort in decreasing order is  + " + endTime4/1000 + " Nanoseconds.");
          //This will check the time for Quicksort (random)
          long startTime5 = System.nanoTime();
          partition(a, 0, a.length-1);
          long endTime5= System.nanoTime() - startTime5; 
          System.out.println("The running time for the Quicksort in random order is  + " + endTime5/1000 + " Nanoseconds.");
    
          
       }
      //This is the sorting method for Selection Sort
      public static void sort(int[] a){ // Sort a[] into increasing order.
          int N = a.length; // array length
          for (int i = 0; i < N; i++){ // Exchange a[i] with smallest entry in a[i+1...N).
             int min = i; // index of minimal entr.
             for (int j = i+1; j < N; j++)
                if (less(a[j], a[min])) min = j;
                exch(a, i, min);
          }
       }
       private static void exch(int[] a, int i, int j){ 
          int t = a[i]; a[i] = a[j]; a[j] = t; 
       }
       private static boolean less(Comparable v, Comparable w){ 
          return v.compareTo(w) < 0; 
       }
      
      
      //This sorting method will be used for Quicksort
       /*public static void sort1(int[] a){
          StdRandom.shuffle(a); // Eliminate dependence on input.
          sort(a, 0, a.length - 1);
       }*/
       
       //This is the Quicksort Method
       private static void sort(int[] a, int lo, int hi){
          if (hi <= lo) return;
          int j = partition(a, lo, hi); // Partition (see page 291).
          sort(a, lo, j-1); // Sort left part a[lo .. j-1].
          sort(a, j+1, hi); // Sort right part a[j+1 .. hi].
       }
       //The method will be used for partitioning an array so it can be Quicksorted
       private static int partition(int[] a, int lo, int hi){ // Partition into a[lo..i-1], a[i], a[i+1..hi].
          int i = lo, j = hi+1; // left and right scan indices
          Comparable v = a[lo]; // partitioning item
          while (true){ // Scan right, scan left, check for scan complete, and exchange.
             while (less(a[++i], v)) if (i == hi) break;
             while (less(v, a[--j])) if (j == lo) break;
             if (i >= j) break;
                exch(a, i, j);
             }
             exch(a, lo, j); // Put v = a[j] into position
             return j; // with a[lo..j-1] <= a[j] <= a[j+1..hi].
          }
          private static void sort3(int[] a, int lo, int hi){ // See page 289 for public sort() that calls this method.
             if (hi <= lo) return;
             int lt = lo, i = lo+1, gt = hi;
             Comparable v = a[lo];
             while (i <= gt){
                int cmp = a[i].compareTo(v);
                if (cmp < 0) exch(a, lt++, i++);
                else if (cmp > 0) exch(a, i, gt--);
                else i++;
             } // Now a[lo..lt-1] < v = a[lt..gt] < a[gt+1..hi].
                sort(a, lo, lt - 1);
                sort(a, gt + 1, hi);
          }
          
       
       }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: int cannot be dereferenced.

    What would I use for a int
    Primitives can use the standard operators such as: < > ==
    See the tutorial: Equality, Relational, and Conditional Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Error: int cannot be dereferenced
    By san1 in forum New To Java
    Replies: 6
    Last Post: 04-03-2014, 02:55 PM
  2. double cannot be dereferenced
    By Garcia65 in forum New To Java
    Replies: 3
    Last Post: 10-17-2012, 10:05 AM
  3. ERROR - char cannot be dereferenced HELP
    By jonytek in forum New To Java
    Replies: 6
    Last Post: 05-04-2011, 06:54 AM
  4. int cannot be dereferenced
    By jackrush in forum Advanced Java
    Replies: 2
    Last Post: 11-12-2010, 08:47 AM
  5. Long Cannot Be Dereferenced?
    By caps_lock in forum New To Java
    Replies: 1
    Last Post: 01-18-2009, 01:49 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •