This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array item selection

 
Matt Hall
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to select members of an array that lie between certain values.

i.e. i have the array here. how would i select only the members that lie between two values. For instance I want to select all elements whose value is between 1 and 13.

DHand1[0] = 3;
DHand1[1] = 4;
DHand1[2] = 15;
DHand1[3] = 18;
DHand1[4] = 25;
DHand1[5] = 30;
DHand1[6] = 31;
DHand1[7] = 35;
DHand1[8] = 37;
DHand1[9] = 39;
DHand1[10] = 42;
DHand1[11] = 45;
DHand1[12] = 51;
 
Stephen Foy
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you want to select certain members of an array with certain values, you should go through a for loop.



Can you see whats going on here? the variable 'i' represents the array element, then it checks if this element meets the criteria of the if statement.

Hope this helps.

[ April 02, 2006: Message edited by: Stephen Foy ]
[ April 02, 2006: Message edited by: Stephen Foy ]
 
Matt Hall
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, I get whats going on here, just one more thing. How do I now select the highest value from the returned statement?

i.e. from the array shown results between 1 and 13 would leave the two elements

DHand1[0] = 3;
DHand1[1] = 4;

How can i then select the highest value out of the 2.

Thanks
 
Stephen Foy
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its quite simple.

 
Matt Hall
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much mate. this works fine.
 
Matt Hall
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have 1 more question on this matter, when I try and output 'highest' it outputs an array that is all zeros, i want it to output a single value. Any ideas.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The array is sorted, don't use a linear search (a for loop) - perform a binary search. There is possibly something in the J2SE API spec. for doing this, but if not, it's not too difficult to throw together.

If this is homework, it is likely that a binary search is what your teacher might be expecting.
 
Matt Hall
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When i try and run the code and output highest with

int highest = 0;
for(int i = 1;i <= dhandi1.length;i++) {
if(dhandi1[i] >= value && dhandi[i] <= value){
if(dhandi[i] > highest)
highest = dhandi1[i];
}
System.out.println(highest);
}

i get an output of

1
1
1
1
1
1
1
1
1
1
1
1
1

then an ArrayIndexOutOfBoundsException
 
Matt Hall
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anybody have any ideas how to implement a binary search on this to return the correct value?
 
Henry Wong
author
Marshal
Pie
Posts: 21125
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I believe when Stephan gave you the code example, he expected you to replace "values" with high and low values -- not just create a new variable in order to get his code to compile.

Henry
 
Matt Hall
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apolgies, the code i posted was incorrect, should be:-

for(int i = 1;i <= DHand1.length;i++) {
if(DHand1[i] >= 1 && DHand1[i] <= 13){
if(DHand2[i] > highest)
highest = DHand1[i];
}
}

The idea is i want to find the highest available value in the array between 1 and 13.
 
Henry Wong
author
Marshal
Pie
Posts: 21125
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's DHand2?
 
Matt Hall
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry should be DHand1, the array, just a typo there.
 
Henry Wong
author
Marshal
Pie
Posts: 21125
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java arrays are zero based. Your index should start with zero, and end one short of the array length... something like this...



Other than that, the program looks fine. The variable "highest" should be set to 4, upon completion of the loop.

Henry
 
Matt Hall
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code is still only outputting an array of zeroes, whereas I want 1 value back from the array, not the array itself.
 
Henry Wong
author
Marshal
Pie
Posts: 21125
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Matt Hall:
The code is still only outputting an array of zeroes, whereas I want 1 value back from the array, not the array itself.


Matt,

It is your program, you have to debug it. From looking at your other posts, I am not even sure if you completely understand the algorithm you are trying to implement.

Look at the loop. How is the algorithm supposed to work? What is actually happening? And what are the differences? Once you understand what is unexpected, you can either add debugging messages to look at what is wrong. Or make a small change. Repeat as necessary.

Try it for a few iterations... It is not as scary as it sounds.

Henry
 
Stephen Foy
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Take a look at what your doing, can you see why your printing out the whole array instead of one value?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic