我希望用户将他们想要的任何整数添加到冒泡排序数组中。在实现这一点时,我无法理解如何执行此操作。任何可以帮助我的编码吗?
import java.util.Arrays;
public class Bubble {
public static void main(String[] args) {
int arraylist[] = {
6, 5, 4, 3, 2, 1
};
System.out.println("Final result:" + Arrays.toString(BubbleMethod(arraylist)));
}
public static int[] BubbleMethod(int[] arr) {
int temp;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 1; j < arr.length - i; j++) {
if (arr[j - 1] > arr[j]) {
temp = arr[j - 1];
arr[j - 1] = arr[j];
arr[j] = temp;
}
}
System.out.println("Result " + (i + 1) + ": " + Arrays.toString(arr));
}
return arr;
}
}
答案 0 :(得分:0)
以下代码将为您提供用户所需的输入,您可以修改上面的代码以使我的代码适合它。
import java.util.*;
public class GetInput
{
Scanner userInput = new Scanner(System.in);
private int howManyInteger;
private ArrayList<Integer> intValues;
public GetInput()
{
intValues = new ArrayList<Integer>();
System.out.println("How many integer you want to enter?");
howManyInteger = userInput.nextInt();
for (int i = 0; i < howManyInteger; i++)
{
System.out.print("Enter value for array "+i+": ");
intValues.add(userInput.nextInt());
}
}
}
答案 1 :(得分:0)
要动态扩展数组,您应该创建一个新数组,复制所有现有数组元素并添加要添加到头部或尾部的值。然后你可以返回新数组。
1)要通过向头部添加值来扩展数组,可以使用以下方法;
// adds an integer to the head of the array, returns array with N+1 length
public static int[] addToArrayHead(int[] array, int value) {
int[] newArray = new int[array.length+1];
newArray[0] = value;
for( int i = 1; i < array.length; i++ )
newArray[i] = array[i];
return newArray;
}
2)要通过向尾部添加值来扩展数组,可以使用以下方法;
// adds an integer to the end of the array, returns array with N+1 length
public static int[] addToArrayTail(int[] array, int value) {
int[] newArray = new int[array.length+1];
int i = 0;
for( ; i < array.length; i++ )
newArray[i] = array[i];
newArray[i] = value; //already incremented with post-increment operator
return newArray;
}
使用这些方法,您可以通过头部或尾部扩展数组,请查看下面的代码。在此示例中,阵列通过添加&#39;#&#39;头和&#39; 99&#39;到尾巴;
import java.util.Arrays;
public class Bubble {
public static void main(String[] args) {
int arraylist[] = { 6, 5, 4, 3, 2, 1 };
System.out.println("Final result:"
+ Arrays.toString(BubbleMethod(arraylist)));
for ( int i = 0 ; i < arraylist.length; i++ )
System.out.print(arraylist[i] + " ");
arraylist = addToArrayHead(arraylist,-5); // add to head
arraylist = addToArrayTail(arraylist, 99); // add to tail
System.out.println("\nUpdated array");
for(int i = 0; i < arraylist.length; i++)
System.out.print(arraylist[i] + " " );
}
public static int[] BubbleMethod(int[] arr) {
int temp;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 1; j < arr.length - i; j++) {
if (arr[j - 1] > arr[j]) {
temp = arr[j - 1];
arr[j - 1] = arr[j];
arr[j] = temp;
}
}
System.out.println("Result " + (i + 1) + ": "
+ Arrays.toString(arr));
}
return arr;
}
// adds an integer to the end of the array, returns array with N+1 length
public static int[] addToArrayTail(int[] array, int value) {
int[] newArray = new int[array.length+1];
int i = 0;
for( ; i < array.length; i++ )
newArray[i] = array[i];
newArray[i] = value; //already incremented with post-increment operator
return newArray;
}
// adds an integer to the head of the array, returns array with N+1 length
public static int[] addToArrayHead(int[] array, int value) {
int[] newArray = new int[array.length+1];
newArray[0] = value;
for( int i = 1; i < array.length; i++ )
newArray[i] = array[i];
return newArray;
}
}
该程序的输出如下;
Result 1: [5, 4, 3, 2, 1, 6]
Result 2: [4, 3, 2, 1, 5, 6]
Result 3: [3, 2, 1, 4, 5, 6]
Result 4: [2, 1, 3, 4, 5, 6]
Result 5: [1, 2, 3, 4, 5, 6]
Final result:[1, 2, 3, 4, 5, 6]
1 2 3 4 5 6
Updated array
-5 2 3 4 5 6 99 0
要进一步完成用户交互,请使用 null数组检查更新扩展数组,如下所示;
// adds an integer to the end of the array, returns array with N+1 length
public static int[] addToArrayTail(int[] array, int value) {
//null array check
if( array == null ) {
array = new int[1];
array[0] = value;
return array;
}
int[] newArray = new int[array.length+1];
int i = 0;
for( ; i < array.length; i++ )
newArray[i] = array[i];
newArray[i] = value; //already incremented with post-increment operator
return newArray;
}
// adds an integer to the head of the array, returns array with N+1 length
public static int[] addToArrayHead(int[] array, int value) {
//null array check
if( array == null ) {
array = new int[1];
array[0] = value;
return array;
}
int[] newArray = new int[array.length+1];
newArray[0] = value;
for( int i = 1; i < array.length; i++ )
newArray[i] = array[i];
return newArray;
}
最新的用户输入交互功能添加代码如下。 请注意,这是完整代码和答案;
import java.util.Arrays;
import java.util.Scanner;
public class BubbleUserInput {
public static void main(String[] args) {
int[] array = initArrayWithUserInput();
System.out.println("User Generated Array;");
System.out.println("*********************");
printArray(array);
System.out.println("Sorted Array;");
System.out.println("*************");
printArray(BubbleMethod(array));
}
public static int[] BubbleMethod(int[] arr) {
int temp;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 1; j < arr.length - i; j++) {
if (arr[j - 1] > arr[j]) {
temp = arr[j - 1];
arr[j - 1] = arr[j];
arr[j] = temp;
}
}
System.out.println("Result " + (i + 1) + ": "
+ Arrays.toString(arr));
}
return arr;
}
// adds an integer to the end of the array, returns array with N+1 length
public static int[] addToArrayTail(int[] array, int value) {
//null array check
if( array == null ) {
array = new int[1];
array[0] = value;
return array;
}
int[] newArray = new int[array.length+1];
int i = 0;
for( ; i < array.length; i++ )
newArray[i] = array[i];
newArray[i] = value; //already incremented with post-increment operator
return newArray;
}
// adds an integer to the head of the array, returns array with N+1 length
public static int[] addToArrayHead(int[] array, int value) {
//null array check
if( array == null ) {
array = new int[1];
array[0] = value;
return array;
}
int[] newArray = new int[array.length+1];
newArray[0] = value;
for( int i = 1; i < array.length; i++ )
newArray[i] = array[i];
return newArray;
}
// initialize the array with user input
public static int[] initArrayWithUserInput() {
Scanner in = new Scanner(System.in);
boolean cont = true;
int[] array = null;
String choice;
while( cont ) {
System.out.println("Want to add element ?");
choice = in.nextLine();
if( choice.equalsIgnoreCase("y") ) {
System.out.println("Please enter an integer");
array = addToArrayTail( array, in.nextInt() );
in.nextLine();
}
else {
System.out.println("Array generated");
cont = false;
}
}
in.close(); // close the resource to prevent resource leak
return array;
}
//prints array to the console
public static void printArray(int[] array) {
for(int i = 0; i < array.length; i++)
System.out.printf("%3d ", array[i]);
System.out.println();
}
}
输出如下;
Want to add element ?
y
Please enter an integer
6
Want to add element ?
y
Please enter an integer
4
Want to add element ?
y
Please enter an integer
5
Want to add element ?
y
Please enter an integer
3
Want to add element ?
y
Please enter an integer
1
Want to add element ?
y
Please enter an integer
2
Want to add element ?
n
Array generated
User Generated Array;
6 4 5 3 1 2
Sorted Array;
Result 1: [4, 5, 3, 1, 2, 6]
Result 2: [4, 3, 1, 2, 5, 6]
Result 3: [3, 1, 2, 4, 5, 6]
Result 4: [1, 2, 3, 4, 5, 6]
Result 5: [1, 2, 3, 4, 5, 6]
1 2 3 4 5 6