在Java中,如何交换向量?

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();
    }
}

在交换代码中,出现“分配的左侧必须是变量”错误。 我该如何修复该代码? 在执行任务时,我不能使用收集包。 我在Google或stackoverflow中发现了很多代码。 BUt,它们尚未解决。 帮助..

zuijian168 回答:在Java中,如何交换向量?

要更改Vector中特定索引处的值,必须使用set()。这是显示交换两个值时如何使用它的示例。

// 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);
本文链接:https://www.f2er.com/3003716.html

大家都在问