在重新排列

时间:2016-02-28 21:54:16

标签: java arraylist

我在接下来的事情上遇到了麻烦:我有一个带有数字的arraylist,我想要反转列表中的数字,如果它有1 2 3,则替换中的值3 2的arraylist 1.为此,我创建了一个名为reordenar()的方法;它将第一个arraylist中的最后一个数字放在第二个arraylist的第一个位置。完成此操作后,我不知道如何让第一个arraylist将其数字替换为第二个。这是我编写的代码。

package firstPackage;
import java.util.*;

public class firstMain {

    public static Object reordenar(List n){
        List secondList = new ArrayList();
        for (int i=0; i<n.size(); i++){
            secondList.add(n.size()-i);
        }
        return n;
    }


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        List firstList = new ArrayList();
        firstList.add("1");
        firstList.add("2");
        firstList.add("3");
        firstList.add("7");
        firstList.add("5");
        firstList.add("6");
        firstList.add("9");
        reordenar(firstList);
        System.out.println(firstList);

    }

}

该代码只打印出正常的顺序,而不是重新排列的顺序。非常感谢你的帮助。

1 个答案:

答案 0 :(得分:1)

您不需要2 import UIKit import QuartzCore enum SlideOutState { case BothCollapsed case LeftPanelExpanded } class ContainerViewController: UIViewController { var centerNavigationController: UINavigationController! var centerViewController: ViewController! var currentState: SlideOutState = .BothCollapsed { didSet { let shouldShowShadow = currentState != .BothCollapsed showShadowForCenterViewController(shouldShowShadow) } } var leftViewController: SidePanelViewController? override func viewDidLoad() { super.viewDidLoad() centerViewController = UIStoryboard.centerViewController() centerViewController.delegate = self // wrap the centerViewController in a navigation controller, so we can push views to it // and display bar button items in the navigation bar centerNavigationController = UINavigationController(rootViewController: centerViewController) view.addSubview(centerNavigationController.view) addChildViewController(centerNavigationController) centerNavigationController.didMoveToParentViewController(self) } } let centerPanelExpandedOffset: CGFloat = 60 extension ContainerViewController: CenterViewControllerDelegate { func toggleLeftPanel() { let notAlreadyExpanded = (currentState != .LeftPanelExpanded) if notAlreadyExpanded { addLeftPanelViewController() } animateLeftPanel(shouldExpand: notAlreadyExpanded) } func toggleRightPanel() { } func addLeftPanelViewController() { if (leftViewController == nil) { leftViewController = UIStoryboard.leftViewController() leftViewController!.categories = Category.allCats() addChildSidePanelController(leftViewController!) } } func addChildSidePanelController(sidePanelController: SidePanelViewController) { view.insertSubview(sidePanelController.view, atIndex: 0) addChildViewController(sidePanelController) sidePanelController.didMoveToParentViewController(self) } func animateLeftPanel(shouldExpand shouldExpand: Bool) { if (shouldExpand) { currentState = .LeftPanelExpanded animateCenterPanelXPosition(targetPosition: CGRectGetWidth(centerNavigationController.view.frame) - centerPanelExpandedOffset) } else { animateCenterPanelXPosition(targetPosition: 0) { finished in self.currentState = .BothCollapsed self.leftViewController!.view.removeFromSuperview() self.leftViewController = nil; } } } func animateCenterPanelXPosition(targetPosition targetPosition: CGFloat, completion: ((Bool) -> Void)! = nil) { UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: .CurveEaseInOut, animations: { self.centerNavigationController.view.frame.origin.x = targetPosition }, completion: completion) } func showShadowForCenterViewController(shouldShowShadow: Bool) { if (shouldShowShadow) { centerNavigationController.view.layer.shadowOpacity = 0.8 } else { centerNavigationController.view.layer.shadowOpacity = 0.0 } } } private extension UIStoryboard { class func mainStoryboard() -> UIStoryboard { return UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()) } class func leftViewController() -> SidePanelViewController? { return mainStoryboard().instantiateViewControllerWithIdentifier("LeftViewController") as? SidePanelViewController } class func centerViewController() -> ViewController? { return mainStoryboard().instantiateViewControllerWithIdentifier("CenterViewController") as? ViewController } } 来完成这项工作。只需迭代你ArrayList的一半元素,并使用临时变量在第一个和最后一个之间交换位置,第二个和第二个形成结尾等。

使用ArrayList而不提供它将包含的特定类型也不是一个好主意:

ArrayList

前者(没有特定类型)暗示List<String> firstList = new ArrayList<String>(); 代表ObjectArrayList通常太宽而无法使用。