Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Boolean Array and sorting elements within it

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Boolean Array and sorting elements within it

    If I have a boolean array that contains 30 elements (boolean[] fish), how do I go about isolating every 10 elements to use for something specific?

    Say there are 30 types of fish stored within the boolean array and 0-9 are fish found specifically in the Indian Ocean, 10-19 are fish found specifically in the Atlantic, and 20-29 are fish specifically found in the Pacific Ocean. And for those 10 fish [0-9], [10-19], [20-29], each is a different color (red, orange, green, blue, white, black, silver, yellow, purple and gold), where the colors and locations of the fish are enum types Colors and Locations.

    How do I go about appointing those characteristics to the fish?

    Ex: elements [0-9] are fish from the Indian Ocean and [0] is red, [1] is orange, [2] is green, [3] is blue, [4] is white, [5] is black, [6] is silver, [7] is yellow, [8] is purple, and [9] is gold.

    elements [10-19] are fish from the Atlantic Ocean and [10] is red, [11] is orange, [12] is green, [13] is blue, [14] is white, [15] is black, [16] is silver, [17] is yellow, [18] is purple, and [19] is gold.

    elements [20-29] are fish from the Indian Ocean and [20] is red, [21] is orange, [22] is green, [23] is blue, [24] is white, [25] is black, [26] is silver, [27] is yellow, [28] is purple, and [29] is gold.


    Will I need to appoint those characteristics in the constructor after initializing fish = new boolean[30]?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Boolean Array and sorting elements within it

    30 types of fish stored within the boolean array
    A boolean variable can only have two values: true or false. It can't store types of fish.

    Please explain why you are trying to use boolean.
    Would another data type be better?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Boolean Array and sorting elements within it

    It's a given in the assignment. We have to use the boolean array. I'm aware it only stores True or False. What my professor wants us to do with that is use the boolean array to return True if the element we are searching for is in the array, and return False if it is not.

    --- Update ---

    So based on what you're saying, I should create another array that is of a String type to store the fish in. But when it comes to searching to see if an element is in the array, how will I implement the boolean array? I'll be working with two different types of arrays.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Boolean Array and sorting elements within it

    return True if the element we are searching for is in the array
    Not sure I understand.
    What is the element that is being searched for
    and what is in the array being searched?

    Is there another array besides the boolean array?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Boolean Array and sorting elements within it

    That's what I'm now confused about. Is there no way to store the different types of fish in the boolean array?

    We're searching for certain types of fish. For example, I need to search the array to see if a red fish from the Atlantic Ocean is part of the array. If it isn't, it would return False. The array is supposed to store all of the different types of fish.

    --- Update ---

    He only gave one array (the boolean array) to work with, but now I'm wondering if I need to go on and create a second array that stores the fish. But if I create the second one, what will be the use of the boolean array and how will I use it in conjunction with the second one I create?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Boolean Array and sorting elements within it

    Is there no way to store the different types of fish in the boolean array
    Yes there is no way. As I said before, boolean variables only hold two values.

    The array is supposed to store all of the different types of fish.
    You need to define an array that holds "types of fish".

    I don't know how you are to use the boolean array.
    One possibility: If there were a list of questions that would be answered by true or false, a boolean array could hold the answers to those questions.The answer to the first question at [0], the second at [1], etc
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Boolean Array and sorting elements within it

    Also posted at: Boolean Array And Sorting The Elements Within It - Java | Dream.In.Code
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: October 31st, 2013, 05:31 AM
  2. [SOLVED] matching elements of one array with another array.
    By shenaz in forum Java Theory & Questions
    Replies: 13
    Last Post: March 29th, 2013, 10:31 AM
  3. Replies: 0
    Last Post: January 22nd, 2013, 10:15 PM
  4. Missing elements in array
    By frozen java in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 10th, 2012, 11:52 PM

Tags for this Thread