|
-
September 21st, 2009, 05:27 AM
#1
arrays problem
I'd like to know if by initializing an array it would give you 0000000? when no values are stored
For example:
final static int [] array = new int[11];
By initializing if i display them out it would look like this in my program.
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
0 0 0 0 0 0 0 0 0 0 0
Question is So if after initializing they become 0, what if a user inserts a value 0 into the array[1] then how do you differentiate it between a stored 0 and an initialized 0. Im quite confused.
---------------------------------------------------------------------------------------------
This is how i did my Insert Method:
Im actually searching for the key 0(since it is 0 when initialized) then insert a number into it. I dont know if doing this is actually correct?
Code:
public static void insert(int[] array, int n) {
int nElems = 11; // number of items
int j; // loop counter
long searchKey; // key of item to search for
Scanner sc = new Scanner(System.in);
System.out.print("Enter new number: ");
n = sc.nextInt();
searchKey = 0; // find item with key 0
for(j=0; j<nElems; j++) // for each element,
if(array[j] == searchKey) // found item?
break; // yes, exit before end
if(j>array.length-1) // at the end?
System.out.println("Array Full"); // yes
else if(j !=nElems)
System.out.println("Found" + searchKey); // no
else if (j == nElems)
System.out.println("Error Number Not Found"+searchKey);
//--------------------------------------------------------------
array[j]=n; //store
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|