Hello Community,
I'm working on an assignment for my data structure & algorithm class. My whole summer of not programming in java made me a extremely rusty. I need help creating an array of objects via a class I created. Never mind what I comment out I will work on that later. Here is my code below.
/** * * * CSC 385 * Module 5 * */ import java.util.*; public class Module5 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); Comparable test = new Comparable(10); for(int i=0 ; i < 10; i++) { // test[i] = new Comparable(); System.out.print(" Input value:" + i + " " ); //test[i] = scan.nextInt(); } } /** public int binarySearch(Comparable[] objArray, Comparable searchObj) { int low = 0; int high = objArray.length - 1; int mid = 0; int temp = 0; while (low <= high){ mid = (low + high) / 2; if (arrayIndex.compareTo(searchObj) < 0) { low = mid + 1; temp = low; } else if (objArray[mid].compareTo(searchObj) > 0) { high = mid - 1; temp = high; } else { return mid; } } return temp; if( temp < 0) { temp+=1; } } */ }
public class Comparable { Object[] objArray; public Comparable( Object objArray, int x) { objArray = this.objArray; objArray = new Comparable[x]; } public Comparable(Object[] o) { objArray = o; } public Comparable(int x) { //objArray = this.objArray; objArray = new Comparable[x]; } public void setArray(int x){ objArray = new Comparable[x]; } }
For some reason when I try to create an object from my class and start to add int values to that array object I receive this error message.
********************************
Array required, but Comparable found
You are using syntax here that suggestions that you are trying access to as array element. The variable you trying is not an array, though.
********************************
It seems my syntax is wrong of creating this array object. I have the right idea of want to store objects in the array, but I don't know what I'm doing wrong. Any suggestions as well as documentation I can review?
Much Appreciated!
Rain_Maker