Java Method with arrays

Java Method with arrays

My assignment is to write a java method, smallestIndex, that takes as its parameters an int array and its size, and returns the index of the (first occurrence of the) smallest element in the array. Also, write a program to test your method.

my main method code is :

public class ArrayMethods {
  //variables
  static Scanner input = new Scanner(System.in);

  //methods
  public static int[] intArray(int size) {
      if (size < 0)
          size = 0;
      int[] numbers = new int[size];
      System.out.println("Please enter "+size+" integers separated by

spaces"); for (int i=0; i

  public static double[] doubleArray(int size) {
      if (size < 0)
          size = 0;
      double[] numbers = new double[size];
      System.out.println("Please enter "+size+" numbers separated by

spaces"); for (int i=0; i

  public static String[] stringArray(int size) {
      if (size < 0)
          size = 0;
      String[] numbers = new String[size];
      System.out.println("Please enter "+size+" words separated by spaces");
      for (int i=0; i<numbers.length; i++) 
          numbers[i] = input.next();
      return numbers;
  }

  public static void printArray(int[] data) {
      for (int i=0; i<data.length; i++)
          System.out.print(data[i] + " ");
      System.out.println();
  }

  public static void printArray(double[] data) {
      for (int i=0; i<data.length; i++)
          System.out.print(data[i] + " ");
      System.out.println();
  }

  public static void printArray(String[] data) {
      for (int i=0; i<data.length; i++)
          System.out.print(data[i] + " ");
      System.out.println();
  }

This is my test code:

public class SmallestIndex { //variables

//methods
public static int SmallestIndex(int[]array, int size){
    int currentSmallestIndex = 0;
    int currentSmallValue = array[0];
    for (int i=1; i<size; i++){
        if (array[i] <currentSmallValue){
            currentSmallValue = array [i];
            currentSmallestIndex = i;

        }
    }

    return currentSmallestIndex;
}
//main method
public static void main (String[] args){
    //variables
    final int SIZE = 20;
    int [] numbers = new int [SIZE];
    int SmallestIndex;
    //prompt user for ints in an array and print 
    numbers = ArrayMethods.intArray(SIZE);
    ArrayMethods.printArray(numbers);
    SmallestIndex = SmallestIndex(numbers, SIZE);
    System.out.println("The smallest index is" + SmallestIndex);

}

}

View Answers

April 11, 2011 at 10:51 AM

import java.util.*;

class ArrayExample11{
    public static int SmallestIndex(int[]array, int size){
    int currentSmallestIndex = 0;
    int currentSmallValue = array[0];
    for(int i=1; i<size; i++){
        if(array[i] <currentSmallValue){
            currentSmallValue = array [i];
            currentSmallestIndex = i;
        }
    }
    return currentSmallestIndex;
}
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        System.out.print("Enter array elements: ");
        int size=5;
        int array[]=new int[size];
        for(int i=0;i<array.length;i++){
            array[i]=input.nextInt();
        }
        System.out.println(SmallestIndex(array,size));
    }
}









Related Tutorials/Questions & Answers:
Java Method with arrays
Java Method with arrays  My assignment is to write a java method..., write a program to test your method. my main method code is : public class...]; currentSmallestIndex = i; } } return currentSmallestIndex; } //main method
Arrays in java
Arrays in java  what is param array in java
Advertisements
java arrays
java arrays  how do you write the code for multipliying 2 double arrays to be in a 3rd double array? here is my code: package employeepay... method @return A reference to an array of doubles. */ public static double
java arrays
java arrays  can i know how can we intilize the three dimentional arrays in java? and how can we assign thae values to that array
java arrays
java arrays  i need a java program to store student details like id,name,addr,marks,average,total..using arrays..input data using scanner class and by using class, object and constructor
Java with Arrays
Java with Arrays  I was given the assignment to create two parallel arrays; both of size 50 (declares 50 as a constant) One is an array of strings... and store it in the arrays. The input for the problem should be provided in a text
java arrays
java arrays  Suppose that you are required to store information about students for the module Data structures and Algorithms. The information... and a StudentApplication class with a main method that keeps an array of student records, displays
Java Arrays
Java Arrays  import java.util.ArrayList; public class try3{ public static void reduce(int[]arr, int len){ for (int i= 0; i<len;i++) { arr[i]--; } len--; } public static void main (String[]args){ int
java; arrays - Java Beginners
java arrays example  How can you create a program, by using arrays and the output would be X. by using char or string.Thank you
Introduction to java arrays
Introduction to java arrays   ... of Arrays in Java Programming language. You will learn how the Array class... arrays. To meet this feature java has provided an Array class which abstracts
Arrays in java 7
This tutorial describes the concept of Arrays in java 7
How to concatenate two arrays in Java?
How to concatenate two arrays in Java?  How to concatenate two arrays in Java
intersection of two java arrays
intersection of two java arrays  I am trying to figure out how to compare two arrays and get the intersection and put this into a third array of the correct size. I know how to copy the two arrays into the third array but am
Iterating java arrays
Iterating java Arrays arrays can be iterated by the for, for each loop. array elements can be added to the list and then it can be iterated by the iterator() method. Example 1 public class
arrays in java - Java Beginners
arrays in java  Hi All, I have two arrays. in this two array some name are same. I want to merge those arrays into single. But while merging I want to delete duplicate entries. How merge those arrays. Thanks, mln15584
Java Arrays Tutorials
(). Browse the following code to Learn Java Arrays in detail...The java.util.Arrays class helps the programmers to manipulating the arrays. It provides the methods to easily manipulate the arrays. Methods provided
Converting java arrays into list
it into the list object. Java Array provides a method asList() which is used... the size of the list. Arrays can be converted by the asList() method... are more tutorials of arrays in Java: Java Array Binary Search
Arrays in java
Arrays in java Arrays are the data structure of java , we use array where we need contiguous memory allocation. Array stores same type of data structure... here : Java Array Tutorial
arrays
arrays  using arrays in methods   Java use arrays in methods import java.util.*; class ArrayExample{ public static int getMaxValue(int[] arr){ int maxValue = arr[0]; for(int i=1;i < arr.length;i
java 2d arrays
java 2d arrays  Create a program that will: a) Create the array and populate it with the given values. 9, 8 2, 17 49, 4 13, 119 2, 19 11, 47.... and if i do it manualy it is wrong. public class Arrays { public static void main
reverse arrays in java
reverse arrays in java  how do i write a program in array of size n*m where both n and m are greater than 20 such that the 1st element of an array becomes the last and vice verse
reverse arrays in java
reverse arrays in java  how do i make a code that can be used to reverse the elements of an array in two dimension such that the last element of an array becomes the first element of the array and the first element of an array
arrays - Java Beginners
]; int num; Write Java statements that do the following: a. Call the method...arrays  1.) Consider the method headings: void funcOne(int[] alpha... the value returned by the method funcSum with the actual parameters,50 and the fourth
Arrays
Arrays   Hi I need help with the following exercises. Exercise 1: Write a Java application in which the user is prompted for the total number... of all the values as well. Exercise 2: Write a Java application in which you
ARRAYS SORTING - Java Interview Questions
ARRAYS SORTING  How To Sort An Array With Out Using Sort Method ?I Want Code?  Hi, Here is the code in java. You can find both Ascending and Descending order code. Ascending order is commented. public class
Arrays - Java Interview Questions
a program that take arrays A & B as input and find missing element in B array
arrays help - Java Beginners
arrays help  Write a program that sorts values. ? The program includes readIntegers() that o reads the user inputs o stores them in an array, and o returns the array   ? The program includes easySort() that o sorts
Arrays
Arrays  I'm new to java and I need help completing the question below... called Rebel.java; 2.Create two arrays. One of them must store integer numbers... the numbers in the second array by 3; 7.Print out the contents of both arrays. 8.Swap
arrays
; int population; double area; } a.write a java statement to create an array... a method called checkPopulation which should display all countries whose population.... write a method called displayAreas() which will display all the country's names
arrays - Java Interview Questions
a program that take arrays A & B as input and find missing element in B array
arrays - Java Beginners
, and only a get method for the employee they are a dependent of 4. Add... a method like createDependentArray(int size) that could at least provide each.... Create an addDependent(String firstName, String lastName) method
arrays
Store a table with students and information (name, ID, password, crypted password, etc) in a multi-dimensional array "stud"  Arrays and Strings: Store a table with students and information (name, ID, password, crypted password
arrays - Java Beginners
the largest and the smallest values in the group. ? Include a static method... in an array, and o Returns the array   ? Include a static method that: o..., and o Returns the largest ? Include a static method that: o Has a parameter
Arrays - Java Beginners
Arrays  I have created a multidimensional array(ten by ten number square) using java and I would like to separate the coumns using the pipe symbol...://www.roseindia.net/java
ARRAYS - Java Beginners
the following link: http://www.roseindia.net/tutorial/java/core/assignSeats.html
Arrays - Java Beginners
(); } } } --------------------------------------------- Read for more information. http://www.roseindia.net/java
how to compare 2 arrays using java?
how to compare 2 arrays using java?  hi can anyone give me the information regarding comparision of arrays.   Java Compare Arrays
Arrays
this and fill an array with the 10 letter answers.This is done in a method
arrays part 2 - Java Beginners
arrays part 2  Question 2: Useful Array Algorithms and Operations (5...: ? A static method search() that: o Has a parameter of integer array and another... method sum() that computes the summation of all the values in the array
Arrays
Implement Java code which takes 2 dimensional integer array as input and prints out heaviest island  Implement Java code which takes 2 dimensional integer array as input and prints out heaviest island
Arrays
Arrays  Write a program in java(BlueJ) to store the numbers in single dimensional array(S.D.A)into another S.D.A in reverse order of the location
Introduction to Java Arrays
Introduction to Java Arrays       In this section you will be introduced to the concept of Arrays... of Arrays in java. Array is the most widely used data structure in java. It can
Introduction to java arrays
Introduction to java arrays       In this section you will be introduced to the concept of Arrays... of Arrays in java. Array is the most widely used data structure in java. It can
reverse two dimensional arrays in java
reverse two dimensional arrays in java  reverse array elements in two dimensional array such that the last element becomes the first
arrays program - Java Interview Questions
arrays program  how to write our own array program to find out n'th highest and n'th least i want source code plz replyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy  Hi Friend, Try the following
guys,, need help,, in java programing,, arrays
guys,, need help,, in java programing,, arrays  create a program where you will input 10 numbers and arrange it in ascending way using arrays
java method
java method  can we declare a method in java like this static { System.loadLibrary("nativelib"); } i have seen this in a java learning E book. i don't understand the static { } plz help me. what kind of method
About Java arrays - Java Beginners
About Java arrays  Hello.........My question is can we create a two dimensional matrix using only one for loop
Java using arrays - Java Beginners
Java using arrays  Write a program to input a possibly integer n(n<=10);followed by n real values into an array a of size n and the program must perform the following: Display back the values input Sort the values
merge sorting in arrays - Java Beginners
merge sorting in arrays  Write a program to insert string or characters to an array and apply merge sorting on this array  Hi Friend, Please visit the following link: http://www.roseindia.net/java/beginners

Ads