I have to write a program that takes the smallest number from a list of numbers and puts it on the first position.

public static void minToFront (ArrayList<Integer> minInteger)
	{
		
		int result = 0;
		int min = minInteger.get(0);
		
		for (int i = 0; i < minInteger.size(); i++)
		{
			if (minInteger.get(i)< min)
			{
				min = minInteger.get(i);
				result = min;
				
			}
		}
		minInteger.add(0, minInteger.remove(result));

I wrote the first part of the code, but now i have to write a program that test the above method. And i don't know how to call the method and set the parameters. I wrote this but is uselles.

import java.util.ArrayList;

public class testVowelsandIntegers 
{
	public static void main (String [] args)
	{
		VowelsandIntegers myList = new VowelsandIntegers();
		
		ArrayList<Integer>  list = new ArrayList<Integer>();
		list.add(19);
		list.add(6);
		list.add(15);
		list.add(3);
		
		myList.minToFront(list);
		System.out.printf("Display numbers\n", myList.minInteger);
	}

}

Recommended Answers

All 4 Replies

Hey man.

It might be a good idea in this situation to use a return type.
In this case, your method is 'void' and so returns nothing, but if you made it of type ArrayList<Integer> you could send back the modified list so it could be printed.

For example:

public static ArrayList<Integer> minToFront (ArrayList<Integer> minInteger)
	{
 
		int result = 0;
		int min = minInteger.get(0);
 
		for (int i = 0; i < minInteger.size(); i++)
		{
			if (minInteger.get(i)< min)
			{
				min = minInteger.get(i);
				result = min;
 
			}
		}
		minInteger.add(0, minInteger.remove(result));
                return minInteger;
}

And then for calling it :

import java.util.ArrayList;
 
public class testVowelsandIntegers 
{
	public static void main (String [] args)
	{
		VowelsandIntegers myList = new VowelsandIntegers();
 
		ArrayList<Integer>  list = new ArrayList<Integer>();
		list.add(19);
		list.add(6);
		list.add(15);
		list.add(3);
                list = myList.minToFront(list);
		
		System.out.printf("Display first number (lowest)\n", list.get(0));
	}
 
}

This would get a new list with the min at the front, then you can just print out the first index to see if its the lowest number :)

hope this helps!

Hi thanks for replying so fast.

I tried this as well, but the numbers are still not printing for some reason.
I get the Display text, but not the numbers.

I got it. I simply called list.
Thank for your help.

Hey man, i've been alerted to something pretty stupid i told you to do, it's a bad idea to allocate a value to a variable with a method using that variable as a parameter, like i told you to in this line :

list = myList.minToFront(list);

In fact, the original code you posted would work, with a void method and just calling it like this

myList.minToFront(list);

or alternatively, allocate the value of the return type to another variable, but this is not necessary.

ArrayList<Integer> newList = myList.minToFront(list);

Sorry about that, was just me being silly, but hope its still working and I haven't confused you too much :p

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.