Getting Largest Int From an Array of Integers

public class TestMe
    public TestMe(int ai[])
        _numbers = ai;
public int getBiggest1()
        int i = _numbers[0];
        for(int j = 1; j < _numbers.length; j++)
            if(_numbers[j] >= _numbers[j - 1] && i < _numbers[j])
                i = _numbers[j];
        return i;
    public int getBiggest2()
        double d = Math.random();
        int i = _numbers[0];
        for(int j = 1; j < _numbers.length; j++)
            if(d >= 0.10000000000000001D && i < _numbers[j])
                i = _numbers[j];
        return i;
  private TestMe()
    private int _numbers[];
}can anyone see what is wrong with either of the two methods as regards returning the largest integer from an array of integers.
As far as I can see they work no matter what ints are in the array.
Any ideas anyone ?
Regards
Sean

I used the following client program to use ur class:
I also modified ur class a bit and made the other constructor (the one with no arguments) as public to extend the class.
Both the functions, viz, getBiggest1() and getBiggest2() are working fine...
Now, I am new to JAVA so I might have made mistakes. Please correct me if there is anything wrong.
/*Here is ur class that i have modified*/
public class GetBiggest {
     public GetBiggest(int ai[])     {
          _numbers = ai;   
     public int getBiggest1()     {
          int i = _numbers[0];
          for(int j = 1; j < _numbers.length; j++)           
          if(_numbers[j] >= _numbers[j - 1] && i < _numbers[j])
               i = _numbers[j];
          return i;
     public int getBiggest2() {       
          double d = Math.random();
          int i = _numbers[0];       
          for(int j = 1; j < _numbers.length; j++)           
          if(d >= 0.10000000000000001D && i < _numbers[j])
               i = _numbers[j];
          return i;
public GetBiggest()     {
private int _numbers[];
/*Ur class ends here*/
/*My client program starts here*/
import java.lang.*;
public class TestBiggest extends GetBiggest
     public static void main (String args[])
          int[] arrNums = {30, 20, 40, 10};
          GetBiggest GB = new GetBiggest(arrNums);
          int greatest = 0;
          try
               greatest = GB.getBiggest2();
               System.out.println("\nBiggest number is : " + greatest);
          }//end try block
          catch(Exception e)
               System.out.println("Some error");
          }//end catch block
     }//end main
}//end class
/*End of my client program*/

Similar Messages

  • Constructing a linked list from an array of integers

    How do I create a linked list from an array of 28 integers in a constructor? The array of integers can be of any value that we desire. However we must use that array to test and debug methods such as getFirst(), getLast(), etc...
    I also have a method int getPosition(int position) where its suppose to return an element at the specified position. However, I get an error that says cannot find symbol: variable data or method next()
    public int getPosition(int position){
         LinkedListIterator iter=new LinkedListIterator();
         Node previous=null;
         Node current=first;
         if(position==0)
         return current.data;
         while(iter.hasMore()){
         iter.next();
         if(position==1)
         return iter.data;
         iter.next();
         if(position==2)
         return iter.data;
         iter.next();
         if(position==3)
         return iter.data;
         iter.next();
         if(position==4)
         return iter.data;
         iter.next();
         if(position==5)
         return iter.data;
         iter.next();
         if(position==6)
         return iter.data;
         iter.next();
         if(position==7)
         return iter.data;
         iter.next();
         if(position==8)
         return iter.data;
         iter.next();
         if(position==9)
         return iter.data;
         iter.next();
         if(position==10)
         return iter.data;
         iter.next();
         if(position==11)
         return iter.data;
         iter.next();
         if(position==12)
         return iter.data;
         iter.next();
         if(position==13)
         return iter.data;
         iter.next();
         if(position==14)
         return iter.data;
         iter.next();
         if(position==15)
         return iter.data;
         iter.next();
         if(position==16)
         return iter.data;
         iter.next();
         if(position==17)
         return iter.data;
         iter.next();
         if(position==18)
         return iter.data;
         iter.next();
         if(position==19)
         return iter.data;
         iter.next();
         if(position==20)
         return iter.data;
         iter.next();
         if(position==21)
         return iter.data;
         iter.next();
         if(position==22)
         return iter.data;
         iter.next();
         if(position==23)
         return iter.data;
         iter.next();
         if(position==24)
         return iter.data;
         iter.next();
         if(position==25)
         return iter.data;
         iter.next();
         if(position==26)
         return iter.data;
         iter.next();
         if(position==27)
         return iter.data;
         iter.next();
         if(position==28)
         return iter.data;
         if(position>28 || position<0)
         throw new NoSuchElementException();
         }

    How do I create a linked list from an array of 28 integers
    in a constructor? In a LinkedList constructor? If you check the LinkedList class (google 'java LinkedList'), there is no constructor that accepts an integer array.
    In a constructor of your own class? Use a for loop to step through your array and use the LinkedList add() method to add the elements of your array to your LinkedList.
    I get an error that
    says cannot find symbol: variable data or method
    next()If you look at the LinkedListIterator class (google, wait for it...."java LinkedListIterator"), you will see there is no next() method. Instead, you typically do the following to get an iterator:
    LinkedList myLL = new LinkedList();
    Iterator iter = myLL.iterator();
    The Iterator class has a next() method.

  • Creating colour image map from an array of integers

    not really that relvant to swing, or in fact java even but i wasnt sure where to ask it so:
    i have an array of integers that represents information about each pixel in an image. i.e. for every pixel there is an integer value.
    at the moment i create another image from a quantised version of these integer values, which gives me a greyscale image, where black is low intensity info and white is high.
    however what i want to do is create a colour image like you see, for example, on weather forecasts. where low intensity info is blue, then purple, then red, orange, yellow and then white being the highest.
    i need some kind of algorithm that does this. does anybody have some code that does this already?
    thanks
    kevin

    sorry for not replying sooner sniper. thanks for your help but i have solved the problem:
    thanks to my mate marc for the code.
    import java.awt.Color;
    /** Class to assist in assigning color to a mathematical value.
    *  Data values must be normalized.
    *  @author Marc Ramsdale
    *  Written 20th August 2003
    public class ColorScale {
         /** Receives a normalized value (between 0.0 - 1.0)
          *  The constructor then creates a colour according
          *  to the selected colour scale.
          *  If value is outside range 0.0 - 1.0 colour is java.awt.Color.BLACK
          *  @param double value - (between 0.0 - 1.0)
         /** Maps the normalized value to a color
          *      BLUE      *
          * GREEN    *      *
          * RED           *
         public static Color generateColour(double value) {
          double red = 0.0;
          double green = 0.0;
          double blue = 0.0;
          Color color;
          int gradient = 4;
          int offset1 = 2;
          int offset2 = 4;
              if((value >= 0.0) && (value < 0.25)) {
                   red = 0.0;
                   green = value * gradient;
                   blue = 1.0;               
              else if((value >= 0.25) && (value < 0.5)) {
                   red = 0.0;
                   green = 1.0;
                   blue = -(value * gradient) + offset1;
              else if((value >= 0.5) && (value < 0.75)) {
                   red = (value * gradient) - offset1;
                   green = 1.0;
                   blue = 0.0;
              else if((value >= 0.75) && (value <= 1.0)) {
                   red = 1.0;
                   green = -(value * gradient) + offset2;
                   blue = 0.0;
              return new Color((float)(red), (float)(green), (float)(blue));

  • Get distinct values from plsql array

    Hi,
    I have declared a variable as below in plsql proc.
    type t_itemid is table of varchar2(10);
    inserted set of items in to this using a program
    now i want distinct values from that array how can i get it.

    I am using 9i so i cannot use set operator and more over my problem is that i am declaring the variable inside the plsql block . when i tried i am getting the below errors:
    SQL> r
    1 declare
    2 type t_type is table of varchar2(10);
    3 v_type t_type;
    4 begin
    5 v_type := t_type('toys','story','good','good','toys','story','dupe','dupe');
    6 for i in (select column_value from table(v_type)) loop
    7 dbms_output.put_line(i.column_value);
    8 end loop;
    9* end;
    for i in (select column_value from table(v_type)) loop
    ERROR at line 6:
    ORA-06550: line 6, column 41:
    PLS-00642: local collection types not allowed in SQL statements
    ORA-06550: line 6, column 35:
    PL/SQL: ORA-22905: cannot access rows from a non-nested table item
    ORA-06550: line 6, column 10:
    PL/SQL: SQL Statement ignored
    ORA-06550: line 7, column 22:
    PLS-00364: loop index variable 'I' use is invalid
    ORA-06550: line 7, column 1:
    PL/SQL: Statement ignored

  • Get numerical values from 2D array of string

    Hello! 
    I start by saying that I have a base knowledge of LabView. I'm using LabView 2011. I'm doing some communications test via Can-Bus protocol. As you can see in pictures of the block diagram and of the front panel that I've attached I'm using "Transmit Receive same Port" LabView example. It works very good but I would need to insert the 8 data bytes that I receive from the server in 8 different numerical variables so that I could use them to make some operations. How can I get numerical values from the 2D array of string that I attached? 
    Every example that you can provide is important! 
    Thank you very much! 
    Attachments:
    1.jpg ‏69 KB
    2.jpg ‏149 KB

    Hi martiflix,
    ehm: to unbundle data from a cluster I would use the function Unbundle(ByName)…
    When you are new to LabVIEW you should take all the FREE online resources offered by NI on their website!
    Best regards,
    GerdW
    CLAD, using 2009SP1 + LV2011SP1 + LV2014SP1 on WinXP+Win7+cRIO
    Kudos are welcome

  • Help with getting a set from a array

    if(values<=j)
    I want j to be the last number in the array, how can i get this
    i=array.length-1;
    so its the last number in the array however i want to write j as the last number held in the last array number.
    I know there must be a simple method for this.

    if(values[n]<=j)
    I want j to be the last number in the array, how can
    i get this
    n=array.length-1;
    so its the last number in the array however i want to
    write j as the last number held in the last array
    number.
    I know there must be a simple method for this.Perhaps but I have no idea what you mean...
    int n = array.length -1;okay...
    You want to write j as the last number held in the last array number?
    Do you mean this?
    array[array.length-1] = array.length-1;If not then please restate your question a little more clearly.

  • How to get a value from an array in a nested loop

    Hi
    I have an array inside a four loop. The for loop (it is supposed to run 4 times) is further inside a while loop. The array consists of four elements and the values each of these elements are fed before running the program. To receive the values of each of these elements one by one outside the for loop, I am using the function index array. To receive all the values one by one, I created a shift register and wired the output of the index arrray to it. The problem is that instead of receiving all the values one by one, I am receiving only the value in the fourth
    element of the array...Can someone please help me..

    The attached file is my program.
    Attachments:
    Value from array.vi ‏25 KB

  • Trying to get a value from 1 array based off the ID in another

    Something like this.
    rankGrade = model.rankArray.RANKABBRIVIATION where
    model.rankArray.RANKGRADEID = model.student.RANKGRADEID
    Any suggestions?

    You could use associative arrays, or even better to use XML
    with e4x syntax. See these FB3 help topics:
    Associative arrays
    The E4X approach to XML processing
    myXML.item.(menuName=="small fries").@quantity = "2";

  • Get value from the array based on the HashCode

    public static void runJoin(int[][] t1,int[][] t2)
         PrintWriter out=null;
         int rows = 1000;
         int cols = 7;
         int [][] myTable3 = new int[rows][cols];
         int x = 0;
         System.out.print("Running HashJoin:Method loads the "+
         "smaller table in the memory and applies a hashing function "+
         "to common column and stores it in another table. "+
         "The larger table is then read from the file. "+
         "The same hashing function is applied to Col n of the table and a       matching record in the first table is looked up. A match will create a row in Table 3. ");          
    //Apply hashing function to smaller table and store it in the memory.
              Integer[] It2 = new Integer[t2.length];
              int [] hashCodest2 = new int[t2.length];
              Hashtable ht = new Hashtable();
              for(int i =0; i <t2.length;i++){
                   It2[i] = new Integer(t2[0]);
                   hashCodest2[i] = It2[i].hashCode();
                   ht.put(new Integer(hashCodest2[i]),It2[i]);
              //Larger table get hashcodes
              Integer It1[] = new Integer[t2.length];
              int [] hashCodest1 = new int[t2.length];          
              for(int j =0; j <t1.length;j++){
                   It1[j] = new Integer(t1[j][4]);
                   hashCodest1[j] = It1[j].hashCode();               }
              //Based on the hashcode get the value from the Table2;
              try{
    out = new PrintWriter( new FileOutputStream( "c:\\HashJoinTable.txt" ) );
              Enumeration e = ht.keys();
                   while(e.hasMoreElements())
    //How do I get the value from the array based on the HashCode? Do I need to do a loop here???                         
    hashCodes1.get(e.nextElement());           
              }catch(Exception e){}

    ok I got it......
              //Apply hashing function to smaller table and store it in the memory.
              Integer[] It2 = new Integer[t2.length];
              int [] hashCodest2 = new int[t2.length];
              Hashtable ht = new Hashtable();
              for(int i =0; i <t2.length;i++){
                   It2[i] = new Integer(t2[0]);
                   hashCodest2[i] = It2[i].hashCode();
                   ht.put(new Integer(hashCodest2[i]),It2[i]);
              //Larger table get hashcodes and compare
              Integer It1[] = new Integer[t2.length];
              int [] hashCodest1 = new int[t2.length];          
              Hashtable ht2 = new Hashtable();
              for(int j =0; j <t1.length;j++){
                   It1[j] = new Integer(t1[j][4]);
                   hashCodest1[j] = It1[j].hashCode();               
                   ht2.put(new Integer(hashCodest1[j]),It1[j]);
              //Based on the hashcode get the value from the Table2;
              try{
    out = new PrintWriter( new FileOutputStream( "c:\\HashJoinTable.txt" ) );
              Enumeration e = ht.keys();
              Integer t3[] = new Integer[t2.length];
                   while(e.hasMoreElements())
                        t3[x] = (Integer) ht2.get(e.nextElement());                
                        x++;
              }catch(Exception e){}

  • Get a list of objects from an array

    I'm having trouble getting a list from an array.  I have an array (cardArray) that I load up with movieclips.
    I create the instances via: var card_01:RallyChaseCard = new RallyChaseCard();, card_02 etc.
    I want to be able to run a trace on the array and get a current listing and order of the cards inside.
    When I run trace(cardArray) all I get is a list like this:
    [object RallyChaseCard],[object RallyChaseCard],[object RallyChaseCard]
    How do I get it to list the contents buy their card names?
    This is one of those things that seems like it ought to be so obvious, yet is eluding me and my trial and error attempts to solve it.

    create a name property in your class and assign a name property to your instances (either in the contructor) and/or using getters/setters.  (you'll need a getter anyway.)

  • How to get the values of an Array using JSP Tags

    Hey guys,
    I need some help. I've splited a String using
    fn:split(String, delim) where String = "1,2,3,4" and delim is ,
    This method returns an Array of splited Strings. how do i get the values from this array using jsp tags. I don't wanna put java code to achive that.
    Any help would be highly appreciated
    Thanks

    The JSTL forEach tag.
    In fact if all you want to do is iterate over the comma separated list, the forEach tag supports that without having to use the split function.
    <c:set var="list" value="1,2,3,4"/>
    <c:forEach var="num" items="${list}">
      <c:out value="${num}"/>
    </c:forEach>The c:forTokens method will let you do this with delimiters other than a comma, but the forEach tag works well just with the comma-delimited string.

  • Getting Numerical Vaules from JTextFields

    I'm writing a currency conversion program where the user inputs, say, US Dollars into a JTextBox, and clicks a button. The result will come up as that amount converted into UK Pounds Sterling.
    Whats the best way, upon the user clicking the appropriate button, to take the contents of the JTextField and make them a double? I can't seem to remember a way to convert strings to doubles...any help would be appriciated.

    I know how to get an int from the textfield..
    JTextField txtf = new JTextField("3");
    int number;
    number = Integer.parseInt(txtf.getText());I dont know how to get a double though....

  • HT201269 I have years worth of music on my PC that is about to die.  If I get an iPad is there anyway to get the music from my PC onto the iPad? I have the iCloud but my understanding is only music actually purchased from iTunes lives int he cloud.

    I have years worth of music on my PC that is about to die.  If I get an iPad is there anyway to get the music from my PC onto the iPad? I have the iCloud but my understanding is only music actually purchased from iTunes lives int he cloud. I have music that was legally purchased and loaded to my iTunes account from cd's that I no longer own.  I need a new laptop and am deciding between the iPad and the Mac book.  I use the computer for music, photos, and web - nothing else and nothing fancy.  I don't know that there is a way to save my huge music library without saving it to an external hard drive and then loading it to a new one (does the iPad have a port to plug an external hard drive into?)

    First, you would need to make a backup of the entire iTunes folder on the PC. You could get music to the iPad if the PC was still functioning, or you would have to move the music to another computer. An iPad is not a laptop, and does not have a port to plug an external drive into. You can sync music to the iPad from the computer, but you are correct, unless you get iTunes Match, you would need a computer to copy the music to and then sync to an iPad.

  • Get information from an array saved into a text file

    I am trying to get information from an array saved as a text file to extract the information to build an string. This string will go out from the serial port to a device which will listen this message and it will answer the question. All the information sended sequencially it has to be obtained from that array. Every row contain one instruction and this has to be sended one after one.
    Could you tell me if is possible to make this driver? And if you have a similar driver, Where can I get it from?

    The VIs that I would suggest using are found in the String Function Pallet:
    Spreadsheet String To Array
    Array To Spreadsheet String
    The delimiter should be the termination character. These VIs will adapt to a string array.
    For the Spreadsheet String To Array wire an empty string array and use %s as the format. The delimiter should be the termination character of the text file, most likely a end of line (\r\n).
    For the Array To Spreadsheet String the delimiter should be the termination required for your serial device.
    Using a combination of these two VIs should provide you the functions required to make the driver.

  • Getting a single value from an array collection

    I have an array collection that was created from an XML file
    through the HTTP Service. One of the nodes in the XML file was
    product_number and I can display all of the items in this node in a
    datagrid so I know the array has the name of the node in it.
    I would like to be able to retrieve a single item from the
    array collection (e.g. a product_number = to xxx) and assign it to
    a variable.
    I would also like to be able to assign all the items in a
    particlur column (e.g. all product_numbers) to separate variables
    at the same time.
    Any help would be greatly appreciated.

    You can apply a filterFunction.
    Or you can do it the brute force way: loop over the elements,
    and test for the value you want.
    As far as putting values into variables, I am not sure what
    you want.
    And this is not a Flex Builder question and should go in the
    General Discussion forum.
    Tracy

Maybe you are looking for