-
July 7th, 2005, 01:28 AM
#1
and statement for a for loop
hi there , how do i include a and condition for a for loop
i am wanting to extract out "uyt" and "HostingClientMNC"
but it returns me "java.lang.IndexOutOfBoundsException: Index: 13, Size: 12" error
Code:
arr.add("ns1:ClassificationNode");
arr.add("uyt");
arr.add("lid=urn:oasis:names:tc:ebxml-regrep:SubjectRole:uyt");
arr.add("objectType=urn:oasis:names:tc:ebxml-regrep:ObjectType:RegistryObject:ClassificationNode");
arr.add("status=urn:oasis:names:tc:ebxml-regrep:StatusType:Submitted");
arr.add("id=urn:oasis:names:tc:ebxml-regrep:SubjectRole:uyt");
arr.add("ns1:ClassificationNode");
arr.add("HostingClientMNC");
arr.add("lid=urn:oasis:names:tc:ebxml-regrep:SubjectRole:HostingClientMNC");
arr.add("objectType=urn:oasis:names:tc:ebxml-regrep:ObjectType:RegistryObject:ClassificationNode");
arr.add("status=urn:oasis:names:tc:ebxml-regrep:StatusType:Submitted");
arr.add("id=urn:oasis:names:tc:ebxml-regrep:SubjectRole:HostingClientMNC");
for ( int i = 1; i <= arr.size();)
{
i++;
i++;
i++;
i++;
i++;
i++;
i++;
i++;
System.out.println(arr.get(i));
-
July 7th, 2005, 03:40 AM
#2
Re: and statement for a for loop
Hi,
The reason you are getting the exception is that you are initialising the control variable of your for loop - i to 1, you should init to 0. ie:
Code:
for ( int i = 0; i <= arr.size();)
Remember counting in arrays starts at 0 not 1.
Byron
-
July 7th, 2005, 03:50 AM
#3
Re: and statement for a for loop
yup i tried putting i = 0
still having some errors
Index: 14, Size:12
-
July 7th, 2005, 03:59 AM
#4
Re: and statement for a for loop
Hi,
Sorry, just woke up, wasn't concerntrating properly. Because you don't actually increment variable i in your for loop statement. ie:
Code:
for ( int i = 0; i <= arr.size(); increment would be here normally )
This creates an infinite loop, BUT you are incrementing i inside the loop. Now what is happening is that you have 12 elements in your array and only increment i 8 times, so the for loop executes again because i is still smaller than or equal to arr.size() yet, so by the time you get to
Code:
System.out.println(arr.get(i));
the second time, i is more than all available indexes in your array, does this make sense? This is actually really bad use of a for loop.
Why not loop through the array as normal and test for "uyt" and "HostingClientMNC" with the String equals() method on each iteration of the loop.
Byron
-
July 7th, 2005, 04:33 AM
#5
Re: and statement for a for loop
hi there , thx for the reply .. but those 2 values does is not always there ... they may change ..
-
July 7th, 2005, 04:38 AM
#6
Re: and statement for a for loop
Yes, so if you test for them and they aren't there, then don't take any action ie:
Code:
for(int i =0; i < arr.size(); i++){
if(((String)arr.get(i)).equals("uyt")){
// Do something
}else if(((String)arr.get(i)).equals("HostingClientMNC")){
// Do Something
}
}
-
July 7th, 2005, 11:01 AM
#7
Re: and statement for a for loop
what i meant was that i would not know what the new data thats going to be added in to do the .equals method ...
-
July 7th, 2005, 11:04 AM
#8
Re: and statement for a for loop
OK, then test for the specific index, although that makes no sense, if you know the index of the item you are looking for why bother looping over the array?
Byron
-
July 7th, 2005, 06:45 PM
#9
Re: and statement for a for loop
So you want to do a linear search through the ArrayList to search for 2 String values. There's no need to code an explicit loop to search through the List since the code is already given to you in the form of indexOf():
Code:
int uytPos = arr.indexOf("uyt");
int HostClientMNCPos = arr.indexOf("HostingClientMNC");
if (uytPos > 0) System.out.println(arr.get(uytPos));
else .....;
if (HostClientMNCPos > 0) System.out.println(arr.get(HostClientMNCPos));
else .....;
Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html
-
July 7th, 2005, 09:26 PM
#10
Re: and statement for a for loop
thx for all the reply .. my problem is solved ...
btw .. how can this problem be solved ... whenever i click on the submit button , the value of the combo box already revert back to the first value even if before submitting i clicked the last value ...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|