Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 831343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:11:19+00:00 2026-05-15T04:11:19+00:00

I have two List of array string. I want to be able to create

  • 0

I have two List of array string. I want to be able to create a New List (newList) by combining the 2 lists. But it must meet these 3 conditions:

1) Copy the contents of store_inventory into newList.

2) Then if the item names in store_inventory & new_acquisitions match, just add the two quantities together and change it in newList.

3) If new_acquisitions has a new item that does not exist in store_inventory, then add it to the newList.

The titles for the CSV list are: Item Name, Quantity, Cost, Price.

The List contains an string[] of item name, quantity, cost and price for each row.

    CSVReader from = new CSVReader(new FileReader("/test/new_acquisitions.csv"));
    List <String[]> acquisitions = from.readAll();

    CSVReader to = new CSVReader(new FileReader("/test/store_inventory.csv"));
    List <String[]> inventory = to.readAll();

    List <String[]> newList;

Any code to get me started would be great! =]

this is what i have so far…

        for (int i = 0; i < acquisitions.size(); i++) {
        temp1 = acquisitions.get(i);
        for (int j = 1; j < inventory.size(); j++) {
            temp2 = inventory.get(j);
            if (temp1[0].equals(temp2[0])) {
                //if match found... do something?



                //break out of loop
            }
        }
        //if new item found... do something?
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T04:11:20+00:00Added an answer on May 15, 2026 at 4:11 am

    I would start by building the newList as a HashMap or TreeMap instead of a List. This makes it easy to search for the matching record. Furthermore, I would convert the String[] to a custom object (e.g. Record) that contains the name, quantity, cost and price field. This would take care of copying the information. The you could try something like this:

    Map<String, Record> newMap = new TreeMap<String, Record>();
    for(String[] ss : acquisitions) {
        Record rec = Record.parse(ss); // For requirement (1)
        newMap.put(rec.getName(), rec);
    }
    
    for(String[] ss : inventory) {
        Record rec = Record.parse(ss); // For requirement (1)
        if(newMap.containsKey(rec.getName())) {
            // For requirement (2)
            // The mergeWith method can then add quantities together
            newMap.get(rec.getName()).mergeWith(rec);
        } else {
            // For requirement (3)
            newMap.put(rec.getName(), rec);
        }
    }
    

    edit
    An extra advantage of having a Record object, is that it can be printed to screen much easier by implementing the toString function.

    public class Record implements Comparable<Record> {
        public static Record parse(String[] ss) {
            // TODO: implement some basic parsing
        }
    
        private String name;
        private int quantity;
        private BigDecimal cost, price;
    
        private Record() {}
    
        public String getName() { return name; }
        public int getQuantity() { return quantity; }
        public BigDecimal getCost() { return cost; }
        public BigDecimal getPrice() { return price; }
    
        public int compareTo(Record other) {
            return this.name.compareTo(other.name);
        }
    
        public String toString() {
            return name;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.