Results 1 to 6 of 6
Thread: ArrayList  Logic Error?
 03232014, 09:54 AM #1Member
 Join Date
 Mar 2014
 Posts
 11
 Rep Power
 0
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!
 03232014, 10:30 AM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,414
 Blog Entries
 7
 Rep Power
 25
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,
JosBuild a wall around Donald Trump; I'll pay for it.
 03232014, 10:37 AM #3Member
 Join Date
 Mar 2014
 Posts
 11
 Rep Power
 0
Re: ArrayList  Logic Error?
How do I incorporate the index to the code then? Could you give me an idea? Thanks!
 03232014, 11:02 AM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,414
 Blog Entries
 7
 Rep Power
 25
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,
JosBuild a wall around Donald Trump; I'll pay for it.
 03232014, 11:12 AM #5Member
 Join Date
 Mar 2014
 Posts
 11
 Rep Power
 0
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
 03232014, 11:14 AM #6Member
 Join Date
 Mar 2014
 Posts
 11
 Rep Power
 0
Similar Threads

Logic error in file creation
By Keesjuh in forum New To JavaReplies: 3Last Post: 07172013, 03:14 PM 
Logic Error
By Norris80 in forum New To JavaReplies: 1Last Post: 02182012, 07:32 AM 
logic error?
By droidus in forum New To JavaReplies: 2Last Post: 01212012, 04:17 PM 
help with a logic error
By ShinTec in forum Advanced JavaReplies: 11Last Post: 05022010, 10:19 PM 
Logic Error: simulated accuracy
By Kaito in forum New To JavaReplies: 3Last Post: 10272009, 02:39 PM
Bookmarks