[英]Listing objects in ArrayList in GUI and alternating between many ArrayLists
[英]Alternating between two arraylists.
我很難讓這個東西運行。 我不確定目前為止我所走的路是否正確。 我不太確定哪里在給我一個超出范圍的錯誤。
以下是說明:
編寫一個名為interleave的方法,該方法接受兩個整數a1和a2的ArrayList作為參數,並將a2的元素插入到交替索引處的a1中。 如果列表的長度不相等,則較長列表的其余元素將保留在a1的末尾。 例如,如果a1存儲[10、20、30],a2存儲[4、5、6、7、8],則調用interleave(a1,a2); 應該更改a1來存儲[10、4、20、5、30、6、7、8]。 如果a1已存儲[10、20、30、40、50],而a2已存儲[6、7、8],則調用interleave(a1,a2); 將更改a1來存儲[10、6、20、7、30、8、40、50]。
private static void interleave(ArrayList<Integer> a1,
ArrayList<Integer> a2) {
int i = a1.size();
int j = a2.size();
if (i < j) { // a1 is shorter than a2
for (int k = 0; k < a1.size(); k++) { // before k passes a1 size
a1.add(k+1, a2.get(k));
}
for (int l = a1.size(); l < a2.size(); l++) {
a1.add(a1.size(), a2.get(l));
}
} else if (i > j) { // a1 is longer than a2
for (int k = 1; k < a2.size(); k++) {
a1.add(k+1, a2.get(k));
}
} else { // they are equal length
for (int k = 1; k < a2.size(); k++) {
a1.add(k+1, a2.get(k));
}
}
}
這應該工作
private static void interleave(ArrayList<Integer> a1, ArrayList<Integer> a2) {
int i = -1;
for(Integer elem: a2) {
if(i < a1.size()-1) {
i += 2;
} else {
i += 1;
}
a1.add(i, elem);
}
}
public static void main(String[] args) throws Exception {
ArrayList<Integer> a1 = new ArrayList<>(Arrays.asList(10, 20, 30));
ArrayList<Integer> a2 = new ArrayList<>(Arrays.asList(4, 5, 6, 7, 8));
interleave(a1, a2);
System.out.println(a1);
}
編輯:我必須承認,這段代碼實際上是一個非常糟糕的解決方案,因為對於長列表而言,它將非常慢。 每次將元素添加到a1時,列表的很大一部分都必須移動一個位置。 因此,遵循“ MadProgrammer”的建議,這是一種更好,更快的方法
private static void interleave(ArrayList<Integer> a1, ArrayList<Integer> a2) {
ArrayList<Integer> r = new ArrayList<>(a1.size() + a2.size());
for(int i = 0, j = 0; i < a1.size() || j < a2.size(); i++, j++) {
if(i < a1.size()) r.add(a1.get(i));
if(j < a2.size()) r.add(a2.get(j));
}
a1.clear();
a1.addAll(r);
}
我看到有任何已接受的答案,但我認為這是在使用的情況下, for
適當的索引變量循環會比更方便for
循環,對數組和集合的作品。 例如,查看隨着時間的推移會發生什么。 a2
始終相同: [4 5 6 7 8]
,並且應插入到a1
中的元素的索引為0, 1, 2, 3, 4
。 現在,應該將第一個元素( 4
)插入位置1
a1
中。 之后,需要在位置3
插入下一個元素( 5
)。 然后6
在位置插入需要5
。 通常,需要將a2
第i
個元素插入a1
位置i*2+1
。 這將一直保持,直到i*2+1
大於a1
的元素數量,或者用盡a2
的元素。 考慮到這一點,在插入所有先前的元素之后,將a2
第i
個元素插入到a1
的合適索引是Math.min( i*2+1, l1.size() )
,因為在l1.size()
只是將元素添加到列表的末尾。
我同意關於不給出家庭作業答案的評論 ,但是由於已經有了完整實現的答案,並且我想將for
與一個for-each
循環的索引進行對比,因此我將在此處包括代碼。 更新的interleave
方法是
public static <T> List<T> interleave( final List<T> l1, final List<T> l2 ) {
for ( int i = 0; i < l2.size(); i++ ) {
l1.add( Math.min( i*2+1, l1.size()), l2.get( i ));
}
return l1;
}
不過,這不一定是最有效的實現。 當您到達l1
的末尾並簡單地一次添加所有剩余元素時,完成for
循環會更有意義。 我也把它寫成List
,因為這是所有代碼所依賴的,盡管用get
的隨機訪問只會對有效支持它的列表有效(例如,對於ArrayList
來說會很好,但性能會很差)用於鏈接列表)。 這些僅僅是列表,這一事實也表明您可以考慮使用ListIterator
的解決方案。
在上下文中,並通過一種main
方法進行演示:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Interleave {
public static <T> List<T> interleave( final List<T> l1, final List<T> l2 ) {
for ( int i = 0; i < l2.size(); i++ ) {
l1.add( Math.min( i*2+1, l1.size()), l2.get( i ));
}
return l1;
}
public static void main(String[] args) {
final List<Integer> l1 = new ArrayList<Integer>( Arrays.asList( 10, 20, 30 ) );
final List<Integer> l2 = Arrays.asList( 4, 5, 6, 7 ,8 );
System.out.println( interleave( l1, l2 ));
}
}
在考慮了這一點之后,我認為在這里使用ListIterator
應該說些什么。 它使某些迭代和插入代碼更加簡潔,特別是因為使用ListIterator
插入是在迭代游標之前進行的。 它也不需要任何復雜的索引算法。 這是使用ListIterators
的實現:
public static <T> List<T> interleaveWithIterators( final List<T> l1, final List<T> l2 ) {
// Get an iterator for the l1, and position it after the first element
// or at the end, if there's no first element.
final ListIterator<T> it1 = l1.listIterator();
if ( it1.hasNext() ) { it1.next(); }
// Get an iterator for l2. While there are elements remaining in l2,
// keep adding them to l1 by calling it1.add(). While there are elements
// in l1, this also requires pushing it1 forward by one element on each iteration.
final ListIterator<T> it2 = l2.listIterator();
while ( it2.hasNext() ) {
it1.add( it2.next() );
if ( it1.hasNext() ) { it1.next(); }
}
return l1;
}
為了避免在每次迭代中檢查if ( it1.hasNext() ) { ... }
,可以拆分while循環:
public static <T> List<T> interleaveWithIterators( final List<T> l1, final List<T> l2 ) {
final ListIterator<T> it1 = l1.listIterator();
if ( it1.hasNext() ) { it1.next(); }
final ListIterator<T> it2 = l2.listIterator();
while ( it2.hasNext() && it1.hasNext() ) {
it1.add( it2.next() );
it1.next();
}
while ( it2.hasNext() ) {
it1.add( it2.next() );
}
return l1;
}
這對我有用。
private static void interleave(ArrayList<Integer>
a1, ArrayList<Integer> a2) {
int i = -1;
for(Integer elem: a2) {
if(i < a1.size()-1) {
i += 2;
} else {
i += 1;
}
a1.add(i, elem);
}
}
public static void main(String[] args) throws Exception {
ArrayList<Integer> a1 = new ArrayList<>(Arrays.asList(10, 20, 30));
ArrayList<Integer> a2 = new ArrayList<>(Arrays.asList(4, 5, 6, 7, 8));
interleave(a1, a2);
System.out.println(a1);
}
public static LinkedList<Integer> alternate(LinkedList<Integer> list1, LinkedList<Integer> list2){
List<Integer> newList = new LinkedList<Integer>();
Iterator<Integer> itr1 = list1.iterator();
Iterator<Integer> itr2 = list2.iterator();
while (itr1.hasNext() || itr2.hasNext()){
if (itr1.hasNext()){
newList.add(itr1.next());
}
if (itr2.hasNext()){
newList.add(itr2.next());
}
}
return newList;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.