Solved

JAVA arrays and methods

Posted on 2009-05-18
5
310 Views
Last Modified: 2013-11-23
I need help with fixing up  the code that test the method in the main method.
And finding  the index first  occurance of the smallest index. My program  finds the index of the smallest value in the entire array.


I am writing a program that takes the smallestindex that takes the parameters, a int array and it size  and returns the index of the first ocuurene of the smallest value.
{

double[] alpha = new double[50];

                 System.out.println(smallestindex(alpha));

      {

           public static int smallestIndex(int  alpha[])

			int index = 0;

			int  smallest;

			smallest  = data[0];

			for(i =0; i < data.lenght(); i++)

			if(smallest < data[i])

			{

			smallest = data[i];

			index = i;

			}

			return index;

Open in new window

0
Comment
Question by:Valisha
  • 3
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
That's looking ok, apart from the array being empty as shown. What's the problem?
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
>                         for(i =0; i < data.lenght(); i++)

should be:

                        for(i =0; i < data.length; i++)

0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> double[] alpha = new double[50];

you also don't initialize your array

double[] alpha = new double[] { 1.2, 4.5, 1.7, 9.0, -3.4, 5.6};

or you could use a loop



0
 

Author Comment

by:Valisha
Comment Utility
System.out.println(smallestindex(alpha));
      {
           public static int smallestIndex(int  alpha[])
is this part correct
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
Comment Utility
try this:

public class Smallest {

      public static void main(String[] args)
      {
            int[] alpha = new int[] { 1, 4, 1, 9, -3, 5};
            System.out.println(smallestIndex(alpha));
      }

      public static int smallestIndex(int[] data) {
            int index = 0;
            int smallest;
            smallest = data[0];
            for (int i = 0; i < data.length; i++)
                  if (smallest < data[i]) {
                        smallest = data[i];
                        index = i;
                  }
            return index;
      }

}


0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now