Results 1 to 11 of 11
  1. #1
    hills2720s is offline Member
    Join Date
    Oct 2013
    Posts
    17
    Rep Power
    0

    Default Simple question about array in JAVA

    Hello, I am just a beginner of JAVA. I am confused and want to ask a few question.
    The following is an exercise in a text book.

    Given the following code,

    public void expand(Object a) {
    // assume element has enough capacity
    for (int i = size - 1; i >= 0; i--) {
    element[4 * i + 3] = a;
    element[4 * i + 2] = a;
    element[4 * i + 1] = a;
    element[4 * i] = element[i];
    }
    size = 4 * size;
    }

    element is a one-dimensional array that stores elements of the type Object.
    The data member size is such that the list elements are in positions 0 through size-1 of the array.



    What is the size of array element and what is the output
    after executing the method expand( new Integer(18)) ,
    if the list element[0:2] = [9, 8, 7], and the size is 3,

    (NOTE : Assume that the array element has enough capacity for expansion.)


    Also, i want to ask what does it mean element[0:2], is that (colon) a operator?

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Simple question about array in JAVA

    It's not an operator.
    They're saying element[0] to element[2]. So element[0] is 9, element[1] is 8 and element[2] is 7.

    The question is asking what will the array look like after this method is called with a "new Integer(18)" parameter, and an element array with those values and size 3.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,395
    Rep Power
    25

    Default Re: Simple question about array in JAVA

    what does it mean element[0:2]
    Did you try compiling code with that statement?
    It does not look like a valid java statement. Is the author of the book using some special syntax to describe some situation in one statement instead of coding several valid statements?
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: Simple question about array in JAVA

    You need to try this out on paper. Just pretend you are the JVM executing the compiled code.

    The statement element[0:2] = [9,8,7] is just a prose shorthand for saying the elements from 0 to 2 contain the values 9,8,7.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    hills2720s is offline Member
    Join Date
    Oct 2013
    Posts
    17
    Rep Power
    0

    Default Re: Simple question about array in JAVA

    Thank all of you guys!!
    Now, I know what does it mean.
    but how about the question in that exercise??


    Is the size 12?
    what is the output ?

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: Simple question about array in JAVA

    The size doesn't change, just the number of filled spaces. And there is no output because nothing is displayed or written.
    I do not believe the answer is 12. Did you try this on paper and maintain columns of the various indices? Or you could just add some
    additional code and try it out.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    hills2720s is offline Member
    Join Date
    Oct 2013
    Posts
    17
    Rep Power
    0

    Default Re: Simple question about array in JAVA

    [11] = 18
    [10] = 18
    [9] = 18
    [8] = 7
    [7] = 18
    [6] = 18
    [5] = 18
    [4] = 8
    [3] = 18
    [2] = 18
    [1] = 18
    [0] = 9

    Those are what I've tried on paper.
    um.. maybe I misunderstand the flow of running the program.
    what is the correct flow of that program??

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: Simple question about array in JAVA

    When you execute the following statement.
    Java Code:
    element[4 * i] = element[i];
    //
    i is equal to 2 the first time thru so
    element[8] = element[2];

    So what is element[2] set to initially?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    hills2720s is offline Member
    Join Date
    Oct 2013
    Posts
    17
    Rep Power
    0

    Default Re: Simple question about array in JAVA

    element[8] = element[2] = 7
    element[4] = element[1] = 8
    element[0] = 9

    how about the others?
    element[4 * i + 3] = a;
    element[4 * i + 2] = a;
    element[4 * i + 1] = a;

    is it all equal to "integer a" that initially set to 18?

    and the size is ?

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,600
    Rep Power
    5

    Default Re: Simple question about array in JAVA

    My apologies! I forgot the list was initialized to 9,8,7 as described. Yes, I believe you figure it out correctly.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    hills2720s is offline Member
    Join Date
    Oct 2013
    Posts
    17
    Rep Power
    0

    Default Re: Simple question about array in JAVA

    Quote Originally Posted by jim829 View Post
    My apologies! I forgot the list was initialized to 9,8,7 as described. Yes, I believe you figure it out correctly.

    Regards,
    Jim
    NVM , Thanks for answering.
    I want to ask is that how to compute the time complexity in big-O notation of the
    expand(Object a) method as a function of the list size.

Similar Threads

  1. Java: simple ArrayList question
    By bronzefish in forum New To Java
    Replies: 7
    Last Post: 01-23-2013, 01:47 PM
  2. Simple Java question
    By devinmont in forum New To Java
    Replies: 1
    Last Post: 09-11-2012, 10:15 PM
  3. Simple array question..
    By Arigo in forum New To Java
    Replies: 13
    Last Post: 04-22-2011, 03:18 AM
  4. Replies: 6
    Last Post: 02-17-2011, 01:22 PM
  5. pretty simple array question
    By 711groove in forum New To Java
    Replies: 8
    Last Post: 12-06-2009, 10:14 AM

Posting Permissions

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