如何动态在DataList每3个元素后插入AdsList对应元素?
Got it, let's turn that manual insertion into a clean, dynamic solution. Here's how you can automate adding ads from your AdsList into DataList after every 3 jobs:
Approach 1: Create a Merged New List (Safer, No Modifications to Original Lists)
This method builds a brand new list with your jobs and ads combined, leaving your original DataList and AdsList untouched. Perfect if you need to keep the original data intact.
First, let's assume your Ads and Jobs classes look something like this (adjust if your actual classes differ):
class Ads { private String name; public Ads(String name) { this.name = name; } @Override public String toString() { return name; } // For easy testing/printing } class Jobs { private String name; public Jobs(String name) { this.name = name; } @Override public String toString() { return name; } }
Now the dynamic merging function:
public static List<Object> mergeJobsWithAds(List<Jobs> jobList, List<Ads> adsList) { List<Object> mergedList = new ArrayList<>(); int adsCounter = 0; int jobCount = 0; for (Jobs job : jobList) { mergedList.add(job); jobCount++; // Insert an ad after every 3 jobs, if we still have ads left if (jobCount % 3 == 0 && adsCounter < adsList.size()) { mergedList.add(adsList.get(adsCounter)); adsCounter++; } } return mergedList; }
How to Use It
public static void main(String[] args) { // Initialize your ads and jobs List<Ads> adsList = new ArrayList<>(); adsList.add(new Ads("Ads1")); adsList.add(new Ads("Ads2")); adsList.add(new Ads("Ads3")); List<Jobs> jobList = new ArrayList<>(); for (int i = 1; i <= 10; i++) { jobList.add(new Jobs("Jobs" + i)); } // Get the merged result List<Object> finalList = mergeJobsWithAds(jobList, adsList); // Print to verify (matches your manual implementation) for (Object item : finalList) { System.out.println(item); } }
Approach 2: Modify the Original Data List (Memory Efficient)
If you prefer to modify your existing DataList directly (instead of creating a new list), use this method. Just make sure your DataList is typed to accept both Jobs and Ads (e.g., List<Object> or a common parent type).
public static void insertAdsIntoJobsList(List<Object> jobList, List<Ads> adsList) { int adsIndex = 0; // First insertion position is after the 3rd job (index 3, 0-based) int insertPos = 3; // Keep inserting until we run out of ads or pass the end of the job list while (insertPos <= jobList.size() && adsIndex < adsList.size()) { jobList.add(insertPos, adsList.get(adsIndex)); adsIndex++; // After inserting an ad, the next insertion position is 4 spots ahead (3 new jobs + 1 ad) insertPos += 4; } }
How to Use This Method
public static void main(String[] args) { List<Ads> adsList = new ArrayList<>(); adsList.add(new Ads("Ads1")); adsList.add(new Ads("Ads2")); adsList.add(new Ads("Ads3")); // Use List<Object> to hold both Jobs and Ads List<Object> jobList = new ArrayList<>(); for (int i = 1; i <= 10; i++) { jobList.add(new Jobs("Jobs" + i)); } // Insert ads directly into the job list insertAdsIntoJobsList(jobList, adsList); // Verify the result for (Object item : jobList) { System.out.println(item); } }
Key Notes for Edge Cases
- Fewer than 3 jobs: No ads will be inserted.
- Out of ads: We stop inserting once we've used all ads in
AdsList, even if there are more jobs left. - Exact multiple of 3 jobs: The last ad will be inserted right after the final job.
内容的提问来源于stack exchange,提问作者android2892

