• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

arrays are like strings which are objects!

 
Mike Smith
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
I had another question this time regarding arrays. Ok, I understand that
arrays are like strings(both of which are objects).I understand that arrays
are always objects no matter if they hold either primitives or references to
objects such as strings. However, since arrays are objects does that mean
that if an element of an array is holding primitives such as integers, do
you still need to reference it, is the actual value stored in a variable
other than the element in the array. For example, in the following piece of
code what is happening?
I think this is happening is this the correct logic or rather reasoning and
understanding? Please someone correct me if I am wrong.

public static void main(String[] args) {
int i;
int j;
int temp;
int num = 0;
int [] list; *** declare an int array variable called list that can hold only integers and the variable list can potentially reference a string object once
created.

Random rand = new Random();

// generate random integer between 15 and 25
num = rand.nextInt( 11 ) + 15;

// allocate an array of that size
list = new int[num]; *** assign the value of num to the array size. That is create a array object called list and assign it a size that is equal to num.

// generate that many random integers between 0 and 99
for( i = 0; i < num; i = i + 1 ) { ***This is the part I am most confused at. I see i<num .Is this where the actually numbers of the size of array gets stored. OR are the values of the integers just being stored in the element of the array indexed at num.?
list[i] = rand.nextInt( 100 );
}
Any help much appreciated. I have to make a tic tac toe game by friday and
need to understand arrays completely.
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike Smith:
public static void main(String[] args) {
int i;
int j;
int temp;
int num = 0;
int [] list; *** declare an int array variable called list that can hold only integers and the variable list can potentially reference a string object once created.

An int array may only contain ints, no Strings.

Random rand = new Random();

// generate random integer between 15 and 25
num = rand.nextInt( 11 ) + 15;

// allocate an array of that size
list = new int[num]; *** assign the value of num to the array size. That is create a array object called list and assign it a size that is equal to num.

Yes

// generate that many random integers between 0 and 99
for( i = 0; i < num; i = i + 1 ) { ***This is the part I am most confused at. I see i < num. Is this where the actually numbers of the size of array gets stored. OR are the values of the integers just being stored in the element of the array indexed at num.?

There are num elements in the array that you created above. As you iterate through the array, 1st element, 2nd element, 3rd element, you are assigning rand.nextInt(100) to the element at that position. You want to stop at list[num - 1] so you don't get an ArrayIndexOutOfBoundsException.

list[i] = rand.nextInt( 100 );
}
Any help much appreciated. I have to make a tic tac toe game by friday and
need to understand arrays completely.[/QB]
 
Campbell Ritchie
Sheriff
Posts: 48386
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right that arrays are objects. If you look in Deitel's book (6th edition pages 303-5), you find out that you can pass
  • An array, eg myObject.myMethod(myArray); passes the whole array
  • OR: an element of the array, eg myObject.myMethod(myArray[23]); passes no 23, ie the 24th member of the array
  • Now, if you pass an object, which means the whole array, or an object member, you pass it by reference; you are telling the receiving method "you will find all the details at memory ref 1234abc," whereas if you pass a primitive value, if for example your array is an int[] array, you might pass an int value, say "your value is 97."

    And as Marilyn de Queiroz has already told you, when you iterate through the array with a for loop, you use (int i = 0; i < myArray.length; i++), because:
  • The 1st item in an array is numbered 0, not 1.
  • The last item in an array of n elements is numbered n-1. So if you have 100 elements, your last one is no 99.
  • Each array has a public attribute called length. It can be confusing when you are used to objects like Strings which have a length() method remembering whether to use () or not.

  • [ October 18, 2005: Message edited by: Campbell Ritchie ]
    [ October 18, 2005: Message edited by: Campbell Ritchie ]
     
    Adam Richards
    Ranch Hand
    Posts: 135
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think you misunderstand arrays. They are NOT objects. They have no constructors, methods, inheritance, etc. They are similar to strings & collections in that they hold sequences of things, but they are not objects per se. However, there are array-like objects such as ArrayList.

    Hope this clarifies things.
     
    Ernest Friedman-Hill
    author and iconoclast
    Marshal
    Pie
    Posts: 24208
    35
    Chrome Eclipse IDE Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Adam Richards:
    I think you misunderstand arrays. They are NOT objects. They have no constructors, methods, inheritance, etc.


    I'm sorry, Adam, but this is all very wrong. Arrays are objects, and there are inheritance relationships among array types. Arrays do have methods: all the methods of java.lang.Object are available on any array object. And anywhere you can use a java.lang.Object in a program, you can use an array: arrays can be stored in Lists and Maps, for example, or you can simply say

    Object o = new int[3];
     
    Jim Yingst
    Wanderer
    Sheriff
    Posts: 18671
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Adam:
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic