-
reordering an array
I'm trying to compare the elements in an string array to an incoming string value. If this method is called, the string value must always be in rs[0] in the new array like so:
rs[0] = "INSERT DT, NPA, NXX";
rs[1] = "EFF DT, NPA, NXX";
rs[2] = "NPA, EFF DT";
rs[3] = "DUMMY SORT 1";
public String[] keepSortBy()
{
try
{
String strCurrSort = "INSERT DT, NPA, NXX";
String[] ds = new String[4];
ds[0] = "EFF DT, NPA, NXX";
ds[1] = "NPA, EFF DT";
ds[2] = "INSERT DT, NPA, NXX";
ds[3] = "DUMMY SORT 1";
String[] rs = new String[ds.length];
for (int i = 0; i < ds.length - 1; i++)
{
if (!ds[i].equals(strCurrSort))
{
rs[0] = strCurrSort;
rs[i + 1] = ds[i];
} else
{
rs[0] = strCurrSort;
rs[i + 1] = ds[i + 1];
}
}
return rs;
} catch (Exception e)
{
System.out.println("Error in keepSortBy: " + e.toString());
return null;
}
}
If the declaration of the ds array is changed to 3 and the appropriate elements are declared ds[0] - ds[2], the above method works every time no matter where the match occurs in the incoming array.
Any suggestions?
Thanks
-
the problem is in the line
rs[i + 1] = ds[i];
if both arrays are defined to have 4 elements,
then when i is 3,
rs[i+1] would be invalid because rs only goes from 0 to 3.
that also explains why it works if the size of ds is 3, because i + 1 stays within 0 and 3
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
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks