Using arrays

Created arrays/ now what?

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1534 Views - Last Post: 13 May 2009 - 09:24 AM Rate Topic: -----

#1 Dreww68  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 23-April 09

Using arrays

Post icon  Posted 30 April 2009 - 08:14 AM

Hi all,

I really appreciate all your help so far. I have been able to create a class product, then create a java application that invokes the class product. Now I have to take the application and the data thats in it and store this date in arrrays. I have the arrays initialized, I am just not sure how to assign the data into the arrays and print the information to the screen. Any ideas or direction would be appreciated.

Here is what I have thus far. This code compiles and displays the data I want it to display. But I do not think I am actually using the arrays in the program. I think they are waiting on me :ph34r:
// Fig 1.6 Inventory2.java
//create an Inventory object using array of products

public class Inventory2
{
   // main method begins execution of Java application
   public static void main( String args[] )
   {
	 Inventory variables = new Inventory();
	 // declare array named Inventory
	 double Inventory[] = new double [9];

	 //declare array named Inventory1
	 String Inventory1 []= new String [2];

	 System.out.println( "Book Name: " + variables.getbookName());
	 System.out.println( "Book Number: " + variables.getbookNumber());
	 System.out.println( "Books in stock: " + variables.getbookQuantity());
	 System.out.println( "Book Price: " + variables.getbookPrice());
	 System.out.println( "Book Value: " + variables.getbookValue());
	 System.out.println();
	 System.out.println( "Book2 Name: " + variables.getbook2Name());
	 System.out.println( "Book2 Number: " + variables.getbook2Number());
	 System.out.println( "Books2 in stock: " + variables.getbook2Quantity());
	 System.out.println( "Book2 Price: " + variables.getbook2Price());
	 System.out.println( "Book2 Value: " + variables.getbook2Value());
	 System.out.println();
	 System.out.println( "Book3 Name: " + variables.getbook3Name());
	 System.out.println( "Book3 Number: " + variables.getbook3Number());
	 System.out.println( "Book3 in stock: " + variables.getbook3Quantity());
	 System.out.println( "Book3 Price: " + variables.getbook3Price());
	 System.out.println( "Book3 Value: " + variables.getbook3Value());
	 System.out.println();
	 System.out.println( "TotalInventoryValue:" + variables.getTotalInventoryValue());



   } // end main
 } //end class Inventory1



Is This A Good Question/Topic? 0
  • +

Replies To: Using arrays

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Using arrays

Posted 30 April 2009 - 09:55 AM

First off, you can't declare arrays like that. The basic definition of an array looks like this...

<type>[] <identifier> = new <type>[<size>]; -- the types must be the same, check yours.

Second, you assign variables to different indexes of an array the same way as you would normally, except you stick a [INDEX] on the end. Where INDEX is the specifically numbered index that you want to access.

For instance...

// make an array to hold 3 Strings
String[] strings = new String[3];

strings[0] = "hello";
strings[1] = "world";
strings[2] = "!";


You don't have to assign them like that though, you can assign them using a loop of some sort, so that you can have a different sized array for every execution if needed.

You'll also access them using the same fashion...

// using the same array

int firstLength = strings[0].length();
int secondLength = strings[1].length();
int thirdLength = strings[2].length();


The length() method is a String method, so using each individual String in the array, you can access its methods.

Hope this helps!

This post has been edited by Locke: 30 April 2009 - 09:59 AM

Was This Post Helpful? 1
  • +
  • -

#3 Dreww68  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 23-April 09

Re: Using arrays

Posted 30 April 2009 - 11:27 AM

When assigning a size, if I need 4 indexes, wouldn't I only initialize 3?

index(0)
index(1)
index(2)
index(3), = 4 array index spots?????

so, String[] strings = new String[2]; will have 3 index spots?
Was This Post Helpful? 0
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Using arrays

Posted 30 April 2009 - 01:35 PM

Well, you're half right. (assuming I understand you correctly)

When you declare an array with 5 spots, the indexes are 0, 1, 2, 3, 4, because you start counting at zero, instead of 1.

Mine was just an example on declaring and accessing arrays. You can modify it to meet whatever size needs you have. :)
Was This Post Helpful? 0
  • +
  • -

#5 Dreww68  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 23-April 09

Re: Using arrays

Posted 30 April 2009 - 05:34 PM

WOO __ HOOOO,

ok,,..ok,.. sorry, In java I am excited to be even half right

now I have to figure out how to access and display the info in an array with a while loop or if statement.
Was This Post Helpful? 0
  • +
  • -

#6 Dreww68  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 23-April 09

Re: Using arrays

Posted 30 April 2009 - 05:43 PM

Here is the code I have with arrays created in it,..

// Fig 1.6 Inventory2.java
//create an Inventory object using array of products

public class Inventory2
{
   // main method begins execution of Java application
   public static void main( String args[] )
   {
	 Inventory variables = new Inventory();
	 // declare array named Inventory
	  double[]Inventory = {1,2,3,3.25,5.25,10,15.25};

	 //declare array named Inventory1
	  String[]Inventory1 = new String[3];

	   Inventory1[0] = "Book one";
	   Inventory1[1] = "Book two";
	   Inventory1[2] = "Book three";
	  


   } // end main
} //end class Inventory1



I have to get the info to display like this;

Book name= book
Book price=$$$$
Book number=12334
Number of books in stock
Cost of book=$$$$
and so on.

I am not sure how to manipulate the data in and out of the arrays,....
book
Was This Post Helpful? 0
  • +
  • -

#7 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Using arrays

Posted 30 April 2009 - 05:50 PM

Well first off, I would NOT name the double array Inventory, as that is the same name as a class you have.

It seems you have a class of Book...and Inventory -- which I assume to be a list of some kind, handling a bunch of books. You would just need to construct a Book object using the data in your array(s), and then add that to the list, and print out whichever book's info you need.
Was This Post Helpful? 0
  • +
  • -

#8 Dreww68  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 23-April 09

Re: Using arrays

Posted 30 April 2009 - 05:57 PM

I have two arrays; a double and a string. One named Inventory and the other Inventory1.

If the name of an array is the same as a class, will they interfere with each other?
Was This Post Helpful? 0
  • +
  • -

#9 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Using arrays

Posted 30 April 2009 - 05:59 PM

View PostDreww68, on 30 Apr, 2009 - 06:57 PM, said:

If the name of an array is the same as a class, will they interfere with each other?


Off the top of my head, I don't think the compiler will mind...but it gets really confusing for the programmer (and anyone else who reads it...AKA the members of DIC :)) really fast. Overall, I believe it's a style choice...and a really bad one to name them the same thing.

This post has been edited by Locke: 30 April 2009 - 06:00 PM

Was This Post Helpful? 0
  • +
  • -

#10 Dreww68  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 23-April 09

Re: Using arrays

Posted 30 April 2009 - 06:05 PM

so how does java use a while or if loop to print out data in an array(s)???

is java and C++ alot alike?
Was This Post Helpful? 0
  • +
  • -

#11 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Using arrays

Posted 30 April 2009 - 06:07 PM

You can use a for loop too...I'd prefer that, but I'll do both. :)

Well, if we have an array...

int[] numbers = new int[5];

numbers[0] = 0;
numbers[1] = 1;
numbers[2] = 2;
numbers[3] = 3;
numbers[4] = 4;

for (int x = 0; x < numbers.length; x++)
{
    // this will loop for exactly the number of things the array contains

    // you can now perform actions like this.

    System.out.println(numbers[x]);

    // since 'x' changes every time, so will the accessed part of the
    // array.
}


// using the same array

int x = 0; // loop control variable

while (x < numbers.length)
{
    // you can use the same statement as inside the 'for' loop...

    // BUT MAKE SURE to have this next statement at the end.

    x++;
}


When you use the length property of an array, it returns an integer meaning the size...so an array that holds 5 elements will return 5.

Hope this helps!

EDIT: Yes, Java and C++ are a lot alike structurally and syntactically.

This post has been edited by Locke: 30 April 2009 - 06:11 PM

Was This Post Helpful? 1
  • +
  • -

#12 Dreww68  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 23-April 09

Re: Using arrays

Posted 30 April 2009 - 06:15 PM

one last question. When you have int x = ,....
what is "x" representing here?
Was This Post Helpful? 0
  • +
  • -

#13 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Using arrays

Posted 30 April 2009 - 06:18 PM

You mean in the for line? In that case, x is the variable that the loop initializes. It creates a variable -- x, and then you can provide the condition(s) for the loop to keep going (the second component of the for loop), and the action to perform whenever it reaches the closing brace (third portion). If the condition in the second portion is still true after performing that action, then it goes back to the beginning of the loop.

x is also representing your index for accessing your array. Notice that we put x into the array when accessing it, that means the accessed part of the array will change every time the loop executes.

This post has been edited by Locke: 30 April 2009 - 06:20 PM

Was This Post Helpful? 0
  • +
  • -

#14 Dreww68  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 23-April 09

Re: Using arrays

Posted 30 April 2009 - 06:26 PM

so int "x" is one of the variables in my array??? :blink:
Was This Post Helpful? 0
  • +
  • -

#15 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Using arrays

Posted 30 April 2009 - 06:33 PM

No...x is just the variable used to access the indexes of the array.

Different indexes of the array hold different values. x specifies the index to access during each execution of the loop.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2