Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Create 1d array from 2d array

    Hey im working on a code where i need to get a certain column of a 2d array and create a 1d array with the information.

    The information im using is a product list in an aray in thsi format [barcode][price][stock]

    Now i need to grab the bar codes of the files that are under a certain stock.

    I have so far manged to get my code to output the barcodes that are understocked but cannot work out how to put them into an array.

    Heres my code so far:

    Code:
    public long[] understockedProductBarcodes(){
    		long minStockLevel = 500;
                    int maxElements = productBarcodeList.length;
    		long[] understockedBarcode;
    		understockedBarcode = new long[maxElements];
    		int totalColumns = 3;
    		int r = 0;
    		int c = 0;
    		for (r = 0; r < productBarcodeList.length; r++){
    			for (c = 2; c < totalColumns; c++){
    				if (productBarcodeList[r][c] > 0){
    					if (productBarcodeList [r][c] < minStockLevel){
    						System.out.println(productBarcodeList[r][0]);
    					}
    				}
    			}
    		}
    		return understockedBarcode;
    	}
    Last edited by Episkey; 09-06-2011 at 08:47 AM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia!

    Ask a mod to move this thread over to the correct forum.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    All you need to do is add them to your array.

    How did you get this array in the first place? This seems a prime for moving into an Object (as java is an OO language afterall), and use collections:
    PHP Code:
    public class Item
    {
        private 
    long barcode;
        private 
    long price// maybe a float?  Just going by the array you have.
        
    private long stock;

        
    // Setup your accessors and mutators

        
    public boolean isUnderstocked(long toStock)
        {
            return (
    this.stock toStock) <= 0;
        }
    }

    //...
    public Vector<ItemunderstockedProduct(long toStock)
    {
        
    Vector<Item= new Vector<Item>();
        for (
    Item i yourItemCollection)
        {
            if (
    i.isUnderstocked(toStock))
            {
                
    v.add(i);
            }
        }
        return 
    v;

    Just seems easier to treat these as objects instead of three dimensional arrays.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •