File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Array item selection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array item selection" Watch "Array item selection" New topic
Author

Array item selection

Matt Hall
Greenhorn

Joined: Feb 28, 2006
Posts: 24
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

Joined: Oct 17, 2005
Posts: 143
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 ]

Stephen Foy - Microsoft Application Development Consultant
Matt Hall
Greenhorn

Joined: Feb 28, 2006
Posts: 24
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

Joined: Oct 17, 2005
Posts: 143
Its quite simple.

Matt Hall
Greenhorn

Joined: Feb 28, 2006
Posts: 24
Thanks very much mate. this works fine.
Matt Hall
Greenhorn

Joined: Feb 28, 2006
Posts: 24
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

Joined: Sep 24, 2003
Posts: 1608
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.


Tony Morris
Java Q&A (FAQ, Trivia)
Matt Hall
Greenhorn

Joined: Feb 28, 2006
Posts: 24
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

Joined: Feb 28, 2006
Posts: 24
Anybody have any ideas how to implement a binary search on this to return the correct value?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18748
    
  40



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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Matt Hall
Greenhorn

Joined: Feb 28, 2006
Posts: 24
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
Sheriff

Joined: Sep 28, 2004
Posts: 18748
    
  40

What's DHand2?
Matt Hall
Greenhorn

Joined: Feb 28, 2006
Posts: 24
Sorry should be DHand1, the array, just a typo there.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18748
    
  40

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

Joined: Feb 28, 2006
Posts: 24
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
Sheriff

Joined: Sep 28, 2004
Posts: 18748
    
  40

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

Joined: Oct 17, 2005
Posts: 143


Take a look at what your doing, can you see why your printing out the whole array instead of one value?
 
wood burning stoves
 
subject: Array item selection