Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中,如何交换向量中的两个值? import java.util.Scanner; 导入java.util.Vector; 导入java.util.Collections; 公共类Bank实现了BubbleSort{ 私人病媒账户; 公共银行(){ 账户=新向量(); } 公共帐户(){ 扫描仪输入=新扫描仪(系统输入); 整数金额; while(true){ 系统输出打印项次(“输入”); 金额=in.nextInt(); if(amount_Java_Vector_Swap - Fatal编程技术网

在java中,如何交换向量中的两个值? import java.util.Scanner; 导入java.util.Vector; 导入java.util.Collections; 公共类Bank实现了BubbleSort{ 私人病媒账户; 公共银行(){ 账户=新向量(); } 公共帐户(){ 扫描仪输入=新扫描仪(系统输入); 整数金额; while(true){ 系统输出打印项次(“输入”); 金额=in.nextInt(); if(amount

在java中,如何交换向量中的两个值? import java.util.Scanner; 导入java.util.Vector; 导入java.util.Collections; 公共类Bank实现了BubbleSort{ 私人病媒账户; 公共银行(){ 账户=新向量(); } 公共帐户(){ 扫描仪输入=新扫描仪(系统输入); 整数金额; while(true){ 系统输出打印项次(“输入”); 金额=in.nextInt(); if(amount,java,vector,swap,Java,Vector,Swap,要更改向量中特定索引处的值,必须使用。下面是一个示例,演示如何在交换两个值时使用它 import java.util.Scanner; import java.util.Vector; import java.util.Collections; public class Bank implements BubbleSort{ private Vector<Account> accounts; public Bank() { accounts = new Vector<

要更改向量中特定索引处的值,必须使用。下面是一个示例,演示如何在交换两个值时使用它

import java.util.Scanner;
import java.util.Vector;
import java.util.Collections;

public class Bank implements BubbleSort{
 private Vector<Account> accounts;

 public Bank() {
  accounts = new Vector<Account>();
 }
 public void makeAccount() {
  Scanner in = new Scanner(System.in);
  int amount;
  while(true) {
   System.out.println("Input");
   amount = in.nextInt();
   if(amount<0)
    break;
   accounts.add(new Account(amount));

  }
 }
 public void printAccount() {
  for (int i=0;i<accounts.size();i++) {
   System.out.println(accounts.get(i).get());
  }

 }
 @Override
    public void start() {
        // TODO Auto-generated method stub
        for (int i = 0; i < accounts.size() - 1; i++) {
            for (int j = 0; j < accounts.size() - 1 - i; j++)
                if (isGreater(j, j + 1))
                    swap(j, j + 1);
        }
    }
    @Override
    public void swap(int a, int b) {
        // TODO Auto-generated method stub


        Account temp = accounts.get(a);
        accounts.get(a) = accounts.get(b); //error
        accounts.get(b) = temp; //error
    }

    @Override
    public boolean isGreater(int a, int b) {


        // TODO Auto-generated method stub


        return accounts.get(a).get() > accounts.get(b).get();
    }
}

使用set方法,如
accounts.set(a,accounts.get(b))
// temporarily hold onto the item at index "a"
Account temp = accounts.get(a);

// put a copy of the "b" item at the "a" location – you will briefly
// have the "b" value in two places in your vector
accounts.set(a, accounts.get(b));

// set the "b" location to have the temporary item that
// you first got from starting "a" location
accounts.set(b, temp);