Web Analytics Made Easy -
StatCounter Create 1d array from 2d array - CodingForum


No announcement yet.

Create 1d array from 2d array

  • Filter
  • Time
  • Show
Clear All
new 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:

    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){
    		return understockedBarcode;
    Last edited by Episkey; Sep 6, 2011, 02:47 AM.

  • #2
    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
      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
      long barcode;
      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 (

      Just seems easier to treat these as objects instead of three dimensional arrays.
      PHP Code:
      header('HTTP/1.1 420 Enhance Your Calm'); 
      Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)