php 列表调整顺序,调整数据列表内的顺序 | 学步园

调整数据列表内的顺序,现写成扩展方法工具类

public static class CollectionHelper

{

//交换List项的顺序

public static bool ExchangeOrder(this IList list, int sourceID, int newID)

{

if (sourceID >= list.Count || sourceID < 0 || newID >= list.Count || newID < 0 || sourceID == newID) return false;

try

{

var temp = list[sourceID];

list[sourceID] = list[newID];

list[newID] = temp;

return true;

}

catch (Exception)

{

return false;

}

}

///

/// 向上移动

///

///

/// The 数据列表 list

/// The 需要移动的id

/// The 移动的 位数. 默认为1, 几位就向上移动几位

///

public static bool MoveUpper(this IList list, int id, int num = 1)

{

return list.MoveItem(id, id - num);

}

//向下移动

public static bool MoveDown(this IList list, int id, int num = 1)

{

return list.MoveItem(id, id + num);

}

//移动到顶部

public static bool MoveTopper(this IList list, int id)

{

return list.MoveItem(id, 0);

}

//移动到底部

public static bool MoveBottom(this IList list, int id)

{

return list.MoveItem(id, list.Count -1);

}

///

/// 移动

///

///

/// 数据列表

/// 原来的数据ID

/// 移动后数据ID

///

public static bool MoveItem(this IList list, int sourceID, int newID)

{

if (sourceID >= list.Count || sourceID < 0 || newID >= list.Count || newID < 0 || sourceID == newID) return false;

try

{

var temp = list[sourceID];

list.RemoveAt(sourceID);

list.Insert(newID, temp);

return true;

}

catch (Exception)

{

return false;

}

}

}

调用的方法:

class CollectionSample

{

public void Test()

{

List stuList = new List();

Student stu = new Student(1, "zhangSan");

stuList.Add(stu);

stu = new Student(2, "LiSi");

stuList.Add(stu);

stu = new Student(3, "WangWu");

stuList.Add(stu);

stu = new Student(4, "ZhangLiu");

stuList.Add(stu);

string msg = string.Empty;

foreach (Student item in stuList)

{

msg += String.Format("ID: {0}, Name: {1} ", item.id, item.name);

}

//Exchange(stuList);

Move(stuList);

}

private static void Exchange(List stuList)

{

CollectionHelper.ExchangeOrder(stuList, 2, 1);

string newMsg = string.Empty;

foreach (Student item in stuList)

{

newMsg += String.Format("ID: {0}, Name: {1} ", item.id, item.name);

}

}

private static void Move(List stuList)

{

CollectionHelper.MoveUpper(stuList, 2, 3);

string newMsg = string.Empty;

foreach (Student item in stuList)

{

newMsg += String.Format("ID: {0}, Name: {1} ", item.id, item.name);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java2D 是 Java 平台提供的一个 2D 图形 API,可以用来创建各种图形、绘制图像、处理颜色、应用纹理等等。下面是一个简单的例子,展示如何使用 Java2D 创建一个简单的图形。 ```java import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import javax.swing.JFrame; import javax.swing.JPanel; public class MyPanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; // 设置背景颜色 g2d.setBackground(Color.WHITE); // 设置画笔颜色 g2d.setColor(Color.BLACK); // 创建一个矩形 Rectangle rect = new Rectangle(50, 50, 100, 100); // 填充矩形 g2d.fill(rect); } public static void main(String[] args) { JFrame frame = new JFrame("Java2D Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(200, 200); MyPanel panel = new MyPanel(); frame.add(panel); frame.setVisible(true); } } ``` 在上面的代码中,我们继承了 JPanel 类,并重写了它的 paintComponent 方法。在该方法中,我们首先调用了父类的 paintComponent 方法,然后获取 Graphics2D 对象,设置了背景颜色和画笔颜色,并创建了一个矩形,最后使用 fill 方法填充矩形。 在 main 方法中,我们创建了一个 JFrame 对象,设置了标题和大小,创建了一个 MyPanel 对象,并将它添加到 JFrame 中,最后设置 JFrame 可见。运行程序,你会看到一个黑色的矩形在白色的背景上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值