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));
}
}
Ads
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
Advertisements
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
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
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
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
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
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
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
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
Introduction to Java Arrays
Introduction to
Java Arrays
 ....
Structure of
Arrays
Now lets study the structure of
Arrays in
java. Array..., supports
an array of
arrays. In
Java, a two-dimensional array ' x' is an array
Introduction to java arrays
Introduction to
java arrays
 ....
Structure of
Arrays
Now lets study the structure of
Arrays in
java. Array..., supports
an array of
arrays. In
Java, a two-dimensional array ' x' is an array
Arrays
this and fill an array with the 10 letter answers.This is done in a
method
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
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
Structure of Java Arrays
Structure of
Java Arrays
Now lets study the structure of
Arrays in
java. Array
is the most widely used data structure in
java. It can contain multiple values
of the same
Java making a method deprecated
Java making a
method deprecated
java making a
method deprecated
In
Java how to declare a
method depricated? Is it correct to use
depricated... or
method in
Java. Instead of it
you can use alternatives that exists.
Syntax
class method in java
class
method in java How to write a class
method in
Java?
You must read
Java - Class, Object and Methods in
Java