在java中,如何将数组列表元素向右移动,并在固定位置上添加索引
我有一个数组列表,大小为10个元素。我在这里试图实现的是,当我在任何索引处添加元素时,它旁边的元素应该移位,除了在某些固定索引处 例如,假设我的数组列表如下 [10,20,30,40,50,60,70,80,90100] 索引为{0,1,2,3,4,5,6,7,8,9} 固定索引{4,6} 当我从索引10中删除一个元素并添加一个索引3时,输出应如下所示 [10,20,30,100,50,40,70,60,80,90,] 如果您注意到索引{4,6}处的值在添加索引3处的第10个元素后没有变化在java中,如何将数组列表元素向右移动,并在固定位置上添加索引,java,arrays,sorting,arraylist,Java,Arrays,Sorting,Arraylist,我有一个数组列表,大小为10个元素。我在这里试图实现的是,当我在任何索引处添加元素时,它旁边的元素应该移位,除了在某些固定索引处 例如,假设我的数组列表如下 [10,20,30,40,50,60,70,80,90100] 索引为{0,1,2,3,4,5,6,7,8,9} 固定索引{4,6} 当我从索引10中删除一个元素并添加一个索引3时,输出应如下所示 [10,20,30,100,50,40,70,60,80,90,] 如果您注意到索引{4,6}处的值在添加索引3处的第10个元素后没有变化
List<Integer> values = new ArrayList<>();
values.add(10);
values.add(20);
values.add(30);
values.add(40);
values.add(50);
values.add(60);
values.add(70);
values.add(80);
values.add(90);
values.add(100);
System.out.println(values);
values.removeAt(9)
values.add(3,100);
System.out.println(values);
# Output
[10, 20, 30, 40, 50,60,70,80,90,100]
[10, 20, 30, 100, 40,50,60,70,80,90]
List values=new ArrayList();
增加(10);
增加(20);
增加(30);
增加(40);
增加(50);
增加(60);
增加(70);
增加(80);
增加(90);
增加(100);
System.out.println(值);
值。删除(9)
增加(3100);
System.out.println(值);
#输出
[10, 20, 30, 40, 50,60,70,80,90,100]
[10, 20, 30, 100, 40,50,60,70,80,90]
如果有人能建议任何排序方法或集合来实现这一点,这将是非常有帮助的
输入和输出示例:
int[]a=[7,4,3,2,1,5,6,4,8,9],固定索引为{1,2}
删除第6个索引元素(6)并在索引0处添加
输出应如下所示:
[6,4,3,7,2,1,5,4,8,9]我认为唯一可行的方法是循环浏览列表并创建一个新列表,检查两个索引,当它们出现时,不要替换它们,并从旧列表中输入相同的值
您可以使用一种方法,通过该方法传递所需更改的参数和固定的索引,并在该方法中根据传递的参数创建一个新列表。我认为唯一可能的方法是循环遍历列表并创建一个新列表,并检查两个索引,当它们出现时,不会出现替换它们并从旧列表中输入相同的值
您可以使用一种方法,通过该方法传递所需更改的参数和固定的索引,并在该方法中根据传递的参数创建新列表。以下是我利用现有的
ArrayList
类的简单解决方案,不过,如果您定义自己的实现来满足您的需求,这可能是最好的。请注意,如果要确保更多方法符合您定义的规则,则可能需要重写这些方法:
public static class FixableArrayList<T> extends ArrayList<T> {
// ascending and descending views of the fixed indices
private final SortedSet<Integer> fixedAsc;
private final SortedSet<Integer> fixedDec;
public FixableArrayList() {
TreeSet<Integer> treeSet = new TreeSet<>();
fixedAsc = treeSet;
fixedDec = treeSet.descendingSet();
}
public boolean addFixedIndex(int ind) { return fixedAsc.add(ind); }
public boolean removeFixedIndex(int ind) { return fixedAsc.remove(ind); }
public void move(int fromInd, int toInd) {
if (fromInd == toInd) {
return;
}
if (fixedAsc.contains(fromInd)) {
throw new IllegalArgumentException("Cannot remove from fixed index: " + fromInd);
}
if (fixedAsc.contains(toInd)) {
throw new IllegalArgumentException("Cannot add to fixed index: " + toInd);
}
super.add(toInd, super.remove(fromInd));
if (toInd < fromInd) {
// all between `from` and `to` shifted up, swap fixed indices down back into position
// iterate from low (toInd) to high (fromInd)
for (int i : fixedAsc.subSet(toInd, fromInd)) {
super.add(i, super.remove(i + 1));
}
} else {
// all between `from` and `to` shifted down, swap fixed indices up back into position
// iterate from high (toInd) to low (fromInd)
for (int i : fixedDec.subSet(toInd, fromInd)) {
super.add(i, super.remove(i - 1));
}
}
}
}
public static void main(String[] args) {
FixableArrayList<Integer> values = new FixableArrayList<>();
values.addAll(Arrays.asList(10, 20, 30, 40, 50, 60, 70, 80, 90, 100));
// set the indices that you want to remain fixed
values.addFixedIndex(3);
values.addFixedIndex(5);
values.addFixedIndex(9);
System.out.println(values);
values.move(0, 8);
System.out.println(values);
values.move(8, 0);
System.out.println(values);
}
公共静态类FixableArrayList扩展了ArrayList{
//固定索引的升序和降序视图
专用最终分拣集fixedAsc;
专用最终分拣集fixedDec;
公共FixableArrayList(){
TreeSet TreeSet=新树集();
fixedAsc=树集;
fixedDec=树集。下降集();
}
公共布尔addFixedIndex(int ind){return fixedAsc.add(ind);}
公共布尔removeFixedIndex(int ind){return fixedAsc.remove(ind);}
公共无效移动(int-fromInd,int-toInd){
if(fromInd==toInd){
返回;
}
if(固定的附件包含(fromInd)){
抛出新的IllegalArgumentException(“无法从固定索引中删除:“+fromInd”);
}
if(固定的SC.contains(toInd)){
抛出新的IllegalArgumentException(“无法添加到固定索引:“+toInd”);
}
super.add(toInd,super.remove(fromInd));
if(toInd
下面是我利用现有的ArrayList
类的简单解决方案,不过最好是定义自己的实现来满足需要。请注意,如果要确保更多方法符合您定义的规则,则可能需要重写这些方法:
public static class FixableArrayList<T> extends ArrayList<T> {
// ascending and descending views of the fixed indices
private final SortedSet<Integer> fixedAsc;
private final SortedSet<Integer> fixedDec;
public FixableArrayList() {
TreeSet<Integer> treeSet = new TreeSet<>();
fixedAsc = treeSet;
fixedDec = treeSet.descendingSet();
}
public boolean addFixedIndex(int ind) { return fixedAsc.add(ind); }
public boolean removeFixedIndex(int ind) { return fixedAsc.remove(ind); }
public void move(int fromInd, int toInd) {
if (fromInd == toInd) {
return;
}
if (fixedAsc.contains(fromInd)) {
throw new IllegalArgumentException("Cannot remove from fixed index: " + fromInd);
}
if (fixedAsc.contains(toInd)) {
throw new IllegalArgumentException("Cannot add to fixed index: " + toInd);
}
super.add(toInd, super.remove(fromInd));
if (toInd < fromInd) {
// all between `from` and `to` shifted up, swap fixed indices down back into position
// iterate from low (toInd) to high (fromInd)
for (int i : fixedAsc.subSet(toInd, fromInd)) {
super.add(i, super.remove(i + 1));
}
} else {
// all between `from` and `to` shifted down, swap fixed indices up back into position
// iterate from high (toInd) to low (fromInd)
for (int i : fixedDec.subSet(toInd, fromInd)) {
super.add(i, super.remove(i - 1));
}
}
}
}
public static void main(String[] args) {
FixableArrayList<Integer> values = new FixableArrayList<>();
values.addAll(Arrays.asList(10, 20, 30, 40, 50, 60, 70, 80, 90, 100));
// set the indices that you want to remain fixed
values.addFixedIndex(3);
values.addFixedIndex(5);
values.addFixedIndex(9);
System.out.println(values);
values.move(0, 8);
System.out.println(values);
values.move(8, 0);
System.out.println(values);
}
公共静态类FixableArrayList扩展了ArrayList{
//固定索引的升序和降序视图
专用最终分拣集fixedAsc;
专用最终分拣集fixedDec;
公共FixableArrayList(){
TreeSet TreeSet=新树集();
fixedAsc=树集;
fixedDec=树集。下降集();
}
公共布尔addFixedIndex(int ind){return fixedAsc.add(ind);}
公共布尔removeFixedIndex(int ind){return fixedAsc.remove(ind);}
公共无效移动(int-fromInd,int-toInd){
if(fromInd==toInd){
返回;
}
if(固定的附件包含(fromInd)){
抛出新的IllegalArgumentException(“无法从固定索引中删除:“+fromInd”);
}
if(固定的SC.contains(toInd)){
抛出新的IllegalArgumentException(“无法添加到固定索引:“+toInd”);
}
super.add(to输入,super.remove(f
public static void rotate(int[] arr, int k) {
if ((k %= arr.length) != 0) {
k = k < 0 ? arr.length + k : k;
swapSubArr(arr, 0, arr.length);
swapSubArr(arr, 0, arr.length - k);
swapSubArr(arr, arr.length - k, arr.length);
}
}
private static void swapSubArr(int[] arr, int from, int to) {
for (int i = from, j = to - 1; i < j; i++, j--)
swap(arr, i, j);
}
private static void swap(int[] arr, int i, int j) {
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}