Results 1 to 6 of 6
  1. #1
    dinosaur1993 is offline Member
    Join Date
    Mar 2014
    Posts
    11
    Rep Power
    0

    Default ArrayList - Logic Error?

    Java Code:
    public static void main(String[] args) {
            ArrayList<Integer> aList = new ArrayList<>(); //generic type, specific type decided at compile time
    
            for (int i = 0; i <= 99; i++){
                aList.add(i);         
            }
    
            System.out.println(aList.size());
            System.out.println((aList));
            displayArray(aList,3);
            System.out.println(aList.size());
        }
    
        private static void displayArray(ArrayList<Integer> theArray, int number){
            for (int i = 0; i < theArray.size(); i++){
                if (((theArray.get(i)) % number) == 0){
                    System.out.println(i + " is divisible by " + number);
                }
                else if(((theArray.get(i)) % number) != 0){
                    theArray.remove(i);
                }
            }
            
            System.out.println((theArray));
        }

    I want to try and find each element divisible by 3 (without using the iterator). I keep on getting an output like below:

    XML Code:
    100
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
    0 is divisible by 3
    2 is divisible by 3
    4 is divisible by 3
    6 is divisible by 3
    8 is divisible by 3
    10 is divisible by 3
    12 is divisible by 3
    14 is divisible by 3
    16 is divisible by 3
    18 is divisible by 3
    20 is divisible by 3
    22 is divisible by 3
    24 is divisible by 3
    26 is divisible by 3
    28 is divisible by 3
    30 is divisible by 3
    32 is divisible by 3
    34 is divisible by 3
    36 is divisible by 3
    38 is divisible by 3
    40 is divisible by 3
    42 is divisible by 3
    44 is divisible by 3
    46 is divisible by 3
    48 is divisible by 3
    50 is divisible by 3
    52 is divisible by 3
    54 is divisible by 3
    56 is divisible by 3
    58 is divisible by 3
    60 is divisible by 3
    62 is divisible by 3
    64 is divisible by 3
    66 is divisible by 3
    [0, 2, 3, 5, 6, 8, 9, 11, 12, 14, 15, 17, 18, 20, 21, 23, 24, 26, 27, 29, 30, 32, 33, 35, 36, 38, 39, 41, 42, 44, 45, 47, 48, 50, 51, 53, 54, 56, 57, 59, 60, 62, 63, 65, 66, 68, 69, 71, 72, 74, 75, 77, 78, 80, 81, 83, 84, 86, 87, 89, 90, 92, 93, 95, 96, 98, 99]
    67

    Help please! Thanks!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: ArrayList - Logic Error?

    Removing the numbers that cannot be divided by three ruins the list, i.e. the number at index position #i isn't equal to 'i' anymore (hint: pencil and paper)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    dinosaur1993 is offline Member
    Join Date
    Mar 2014
    Posts
    11
    Rep Power
    0

    Default Re: ArrayList - Logic Error?

    How do I incorporate the index to the code then? Could you give me an idea? Thanks!

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: ArrayList - Logic Error?

    You're printing the index assuming that the number at index value #i also equals i; print the number from the List itself instead. Also, don't increment the index value i when you have just deleted a number from the List.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    dinosaur1993 is offline Member
    Join Date
    Mar 2014
    Posts
    11
    Rep Power
    0

    Default Re: ArrayList - Logic Error?

    Cool! Thanks heaps!

    I manage for it to print out that....

    84 is divisible by 3
    87 is divisible by 3
    90 is divisible by 3
    93 is divisible by 3
    96 is divisible by 3
    How can I take off the increment? Could you please show me?

  6. #6
    dinosaur1993 is offline Member
    Join Date
    Mar 2014
    Posts
    11
    Rep Power
    0

Similar Threads

  1. Logic error in file creation
    By Keesjuh in forum New To Java
    Replies: 3
    Last Post: 07-17-2013, 03:14 PM
  2. Logic Error
    By Norris80 in forum New To Java
    Replies: 1
    Last Post: 02-18-2012, 06:32 AM
  3. logic error?
    By droidus in forum New To Java
    Replies: 2
    Last Post: 01-21-2012, 03:17 PM
  4. help with a logic error
    By ShinTec in forum Advanced Java
    Replies: 11
    Last Post: 05-02-2010, 10:19 PM
  5. Logic Error: simulated accuracy
    By Kaito in forum New To Java
    Replies: 3
    Last Post: 10-27-2009, 01:39 PM

Posting Permissions

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