iT邦幫忙

0

陣列內資料覆蓋(使用JAVA)

我使用的語法為java
我有兩個ArrayList命名aList及bList, aList內假設資料有10筆,bList內假設有3筆。
1、aList內是原始的資料。
2、bList內是程式run完的結果。
3、我想拿bList的資料跟aList比對,若ID有一樣的話,則將bList內的資料倒到原本aList內。
例如:
a:{id=1, name:"ASS", false, companyNameA};
b:{id=1, name:"ASS", true, companyNameB}
主要重點是程式跑完後a跟b的false, companyNameA及true, companyNameB會不一樣
若不一樣,我要使用ID去比對,將b的結果倒到a裡面…

請問語法上,該如何寫,可否請高手指點一番

謝謝,若有敘述不瞭解之處,再麻煩請指點我,謝謝

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

3
海綿寶寶
iT邦大神 1 級 ‧ 2017-06-26 18:44:41
最佳解答

參考資料來源

import java.util.*;
class Student{
  int rollno;
  String name;
  boolean isAvail;
  String compname;
  Student(int rollno,String name,boolean avail,String compname){
   this.rollno=rollno;
   this.name=name;
   this.isAvail=avail;
   this.compname=compname;
  }
}

public class TestCollection3{
 public static void main(String args[]){
  /*
   *  Init values
   */
  Student s1=new Student(101,"Sonoo",true,"compnameA");
  Student s2=new Student(102,"Ravi",false,"compnameA");
  Student s3=new Student(103,"Hanumat",true,"compnameB");
  
  ArrayList<Student> a=new ArrayList<Student>();
  a.add(s1); 
  a.add(s2);
  a.add(s3);

  ArrayList<Student> b=new ArrayList<Student>();
  Student s4=new Student(102,"Ravi",true,"compnameB");
  b.add(s4);

  /*
   *  Show all values before process
   */
  System.out.println("Before\n========");
  Iterator<Student> itrbefore=a.iterator();
  while(itrbefore.hasNext()){
    Student st=itrbefore.next();
    System.out.println(st.rollno+" "+st.name+" "+st.isAvail+" "+st.compname);
  }

  /*
   *  Iterate Array b, compare with array A, replace any items with same id(rollno)
   */
  Iterator<Student> itrb=b.iterator();
  while(itrb.hasNext()){
    Student stb=itrb.next();
    Iterator<Student> itra=a.iterator();
    while(itra.hasNext()) {
       Student sta=itra.next();
       if (stb.rollno==sta.rollno) {
          sta.name = stb.name;
          sta.isAvail = stb.isAvail;
          sta.compname = stb.compname;
       }
    }
  }

  /*
   *  Show all values after process
   */
  System.out.println("After\n========");
  Iterator<Student> itrafter=a.iterator();
  while(itrafter.hasNext()){
    Student st=itrafter.next();
    System.out.println(st.rollno+" "+st.name+" "+st.isAvail+" "+st.compname);
  }

 }
}
0
vjcoder
iT邦見習生 ‧ 2021-12-23 22:42:50
import java.util.*;
 
public class Scaler {
 
   public static void main(String[] args) {
       ArrayList<Integer> list = new ArrayList<>();
       list.add(1);
       list.add(2);
       list.add(3);
       list.add(4);
       //Implementing the first way to convert list to array in java
       Object[] res1Arr=list.toArray();
       //Implementing the second way to convert list to array in java
       Integer[] res2Arr = new Integer[4];
       list.toArray(res2Arr);
       //Printing the first array
       System.out.println("Printing 'res1Arr':");
           for(Object obj: res1Arr)
           //Typecasting required for the first method
               System.out.println((Integer)obj);
       //Printing the second array
       System.out.println("Printing 'res2Arr':");
           for(Integer i: res2Arr)
               System.out.println(i);
   }
}

Source: Scaler

我要發表回答

立即登入回答