例えばint data【5】={1,2,3,4,5}があったとして、キーボードで要素数の1を入力したら1,3,4,5,2,というようにループでずれ、次にキーボードで0を入力したら、3,4,5,2,1となります。
字面通り素直に書くならこうでしょうか。
Processing
1 int [ ] array = { 1 , 2 , 3 , 4 , 5 } ;
2
3 int index = 1 ;
4 int tmp = array [ index ] ;
5 for ( int i = index + 1 ; i < array . length ; i + + ) {
6 array [ i - 1 ] = array [ i ] ;
7 }
8 array [ array . length - 1 ] = tmp ;
9 println ( array ) ;
10 println ( ) ;
11
12 index = 0 ;
13 tmp = array [ index ] ;
14 for ( int i = index + 1 ; i < array . length ; i + + ) {
15 array [ i - 1 ] = array [ i ] ;
16 }
17 array [ array . length - 1 ] = tmp ;
18 println ( array ) ;
[0] 1
[1] 3
[2] 4
[3] 5
[4] 2
[0] 3
[1] 4
[2] 5
[3] 2
[4] 1
ループの代わりにarrayCopy
を使うこともできます。
Processing
1 //for(int i = index + 1; i < array.length; i++){
2 // array[i - 1] = array[i];
3 //}
4 arrayCopy ( array , index + 1 , array , index , array . length - index - 1 ) ;
arrayCopy() / Reference / Processing.org
ただこの要件ならリスト(IntList
等)のほうがはるかに簡単です(まあいろいろ罠もありますが^^;
Processing
1 IntList list = new IntList ( 1 , 2 , 3 , 4 , 5 ) ;
2
3 int index = 1 ;
4 int tmp = list . remove ( index ) ; // index番目を削除しその値を返す
5 list . append ( tmp ) ; // 末尾に追加
6 println ( list ) ;
7 println ( ) ;
8
9 index = 0 ;
10 list . append ( list . remove ( index ) ) ; // 1行でもok
11 println ( list ) ;
12
13
14 // intじゃなくて何かのクラスだった場合
15 //import java.util.List;
16
17 //ArrayList<Integer> list = new ArrayList<>(List.of(1, 2, 3, 4, 5));
18
19 //int index = 1;
20 //int tmp = list.remove(index); // index番目を削除しその値を返す
21 //list.add(tmp); // 末尾に追加
22 //println(list);
23 //println();
24
25 //index = 0;
26 //list.add(list.remove(index)); // 1行でもok
27 //println(list);
IntList / Reference / Processing.org
ArrayList / Reference / Processing.org
この箱の移し替えの作業が例に上げた配列のループのように動かすにはどうしたらいいでしょうか?
実際はintの配列ではなくStaffクラスの配列ということはわかりますが、x
とposition
の関係が全く分かりません。
動く図形の上下関係(Z-order)のようなもの??
選んだトランプを右端に回すような処理??
交換したいところが初期値から動かなかったり、交換してほしいところに表示されません。
説明が難しく伝わっわてない部分もあるかもしれませんがどうかお願いいたします。
言葉で説明が難しければintの例と同じように、初期値と期待値を例示するとか(↓のような)
staff [0] [1] [2] [3] [4] x 1 2 3 4 5 position
clickedIndex = 1
↓
staff [0] [1] [2] [3] [4] x 1 3 4 5 2 position
clickedIndex = 0
↓
staff [0] [1] [2] [3] [4] x 3 4 5 2 1 position
あるいは今問題になっている部分を極々単純化した、実行できるプログラムを提示いただけると話が通じやすいです。
追記 配列がそのまま並び順になるように変更できないでしょうか?(例えばこんな)
Processing
1 Card [ ] cards = {
2 new Card ( 5 , #ff0000 ) ,
3 new Card ( 65 , # 00 ff00 ) ,
4 new Card ( 125 , # 0000 ff ) ,
5 new Card ( 185 , #ffff00 ) ,
6 new Card ( 245 , # 00 ffff )
7 } ;
8
9 void setup ( ) {
10 size ( 300 , 200 ) ;
11 }
12
13 void draw ( ) {
14 background ( 255 ) ;
15
16 for ( int i = 0 ; i < cards . length ; i + + ) {
17 fill ( cards [ i ] . c ) ;
18 rect ( cards [ i ] . x , 75 , 50 , 50 ) ;
19 }
20 }
21
22 void mouseClicked ( ) {
23 // mouseXを雑にcardsインデックス(0~4)に変換
24 int index = int ( map ( mouseX , 0 , width , 0 , 5 ) ) ;
25 println ( index ) ;
26
27 // 選択カードを配列の末尾に移動
28 Card tmp = cards [ index ] ;
29 for ( int i = index + 1 ; i < cards . length ; i + + ) {
30 cards [ i - 1 ] = cards [ i ] ;
31 }
32 cards [ cards . length - 1 ] = tmp ;
33
34 int [ ] xx = new int [ cards . length ] ; // x用の配列を用意
35 for ( int i = 0 ; i < cards . length ; i + + ) {
36 xx [ i ] = cards [ i ] . x ; // 詰める
37 }
38 xx = sort ( xx ) ; // ソート
39 for ( int i = 0 ; i < cards . length ; i + + ) {
40 cards [ i ] . x = xx [ i ] ; // 詰め直し
41 }
42 }
43
44 class Card {
45 int x ;
46 color c ;
47 Card ( int x , color c ) {
48 this . x = x ;
49 this . c = c ;
50 }
51 }
さらに追記
Processing
1 int member2 = 5 ;
2 Staff [ ] staff = {
3 new Staff ( 0 , "A" ) ,
4 new Staff ( 50 , "B" ) ,
5 new Staff ( 100 , "C" ) ,
6 new Staff ( 150 , "D" ) ,
7 new Staff ( 200 , "E" ) ,
8 new Staff ( 250 , "F" ) ,
9 new Staff ( 300 , "G" ) ,
10 new Staff ( 350 , "H" ) ,
11 new Staff ( 400 , "I" ) ,
12 new Staff ( 450 , "J" )
13 } ;
14
15
16 void setup ( ) {
17 size ( 500 , 200 ) ;
18 textAlign ( CENTER , CENTER ) ;
19 textSize ( 24 ) ;
20 }
21
22 void draw ( ) {
23 background ( 255 ) ;
24
25 for ( int i = 0 ; i < staff . length ; i + + ) {
26 fill ( 0 ) ;
27 text ( staff [ i ] . c , staff [ i ] . x , 75 , 50 , 50 ) ;
28 noFill ( ) ;
29 rect ( staff [ i ] . x , 75 , 50 , 50 ) ;
30 }
31 }
32
33 void mouseClicked ( ) {
34 int clickedIndex = int ( map ( mouseX , 0 , width , 0 , 10 ) ) ;
35 println ( "clickedIndex:" + clickedIndex ) ;
36
37 if ( clickedIndex <= member2 && clickedIndex >= 0 ) {
38 Staff tmp = staff [ clickedIndex ] ; // clickedIndex番目をとっておく
39 for ( int i = clickedIndex + 1 ; i <= member2 ; i + + ) { // clickedIndexの次からmember2番目も含め回す
40 staff [ i - 1 ] = staff [ i ] ; // 1個前へ
41 }
42 staff [ member2 ] = tmp ; // 抜けたmember2番目にはめる
43
44 int [ ] xx = new int [ member2 + 1 ] ; // member2がインデックス(5)なら要素数は+1です(6)
45 //int[] xx = new int[staff.length]; // ややこしくなるぐらいなら全件見てもいいのではないか?
46 for ( int i = 0 ; i < xx . length ; i + + ) {
47 xx [ i ] = staff [ i ] . x ;
48 }
49
50 xx = sort ( xx ) ;
51
52 for ( int i = 0 ; i < xx . length ; i + + ) {
53 staff [ i ] . x = xx [ i ] ;
54 }
55
56 printArray ( staff ) ; // 配列表示
57 }
58 }
59
60
61 class Staff {
62 int x ;
63 String c ;
64 Staff ( int x , String c ) {
65 this . x = x ;
66 this . c = c ;
67 }
68 @ Override String toString ( ) { // toStringとprintArrayでデバッグしやすくなる
69 return c + " x:" + x ;
70 }
71 }