Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


 

Java Tutorials


 

 

Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Java Language Introduction To Java Arrays

User Comments
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl
  Blink   Furl   Simpy   Y! MyWeb 

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

10 comments so far (post your own) View All Comments Latest 10 Comments:

i do not understand array

Posted by wee hui siang on Tuesday, 09.28.10 @ 12:39pm | #99914

i like this array chapter thanking you

Posted by reva on Wednesday, 05.12.10 @ 15:52pm | #96982

Thnaks for nice site and really its helpful for lots of students and professional persons, I like this site too much.

Reagds,
Rajesh Pandit
Delhi
9015292632

Posted by Rajesh Pandit on Tuesday, 11.10.09 @ 14:53pm | #92467

package cert;

public class ArrayBound {
public static void main(String[] args) {
int[] a = new int[5];
a[0] = 2;
a[1] = 3;
a[2] = 4;
a[3] = 5;
a[4] = 6;
System.out.println(a[5]);
}

}
In the above code the array can store 5 elements. those are from '0' to '4'. But we tried to get the 5th element which crosses the allocated memory is called out of bound and when you try to run this code you will get the following error.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at cert.ArrayBound.main(ArrayBound.java:10)

Posted by Brahmanaidu on Sunday, 08.30.09 @ 04:53am | #90425

java 2 array
Write an application to test utilize arrays in a variety of ways –
• you will be calling all of the methods you write from the main,
• so remember to make them static

Part 1
1A) In the main: Declare and create 2 arrays called nums1 and nums2
to store 15 integers
1B) Declare a third array called nums3 and assign to it 15 values of your choice
(from 1 to 30 – BUT NOT in sorted order ), for example {25,12,6,2,30……}

2A.Write a method called initArray to initialize an array of int to values from
0 to 14 using a loop.
2B.Pass in the nums1 array as a parameter.

3. Write a method called displayArray to display all numbers in an array of int
on one line in this format:
0 1 2 3 4 and so on. Pass in the nums1array as a parameter.

4. In the main: In the main method, call the initArray method passing in
nums1(see//4A to the right). Print an appropriate message(//4B) before calling
the display array(//4C) , so that you r results look like this:
The nums1 array after initializing:
0 1 2 3 4 and so on
The nums3 array after initializing:
25 12 6 2 30 and so on

5. Write a method called genRandom which takes the array nums2 as a parameter and fills it with random numbers between 1 and 100. in a ‘for’ loop, assign this stmt to each element in the array (int)(Math.random()*99)+1;

6. In the main: add a call to the genRandom methods passing in nums2 followed by an appropriate message explaining what the output will be and then call the displayArray method so your output looks like this:
The nums2 array after initializing:
17 4 12 (random numbers – different each time) and so on

7. Write a method called sumNumbers which takes the nums3 array as a parameter. It sums all numbers in the array and returns the sum as the value of the method. (check your sum by adding all the numbers you put in nums3)

8. In the main, add a call to the sumNumbers method, then Print a message telling what the output is followed by the results . For example:
The following is the sum of all numbers in the nums3 array (REMEMBER: when a method returns a value, you need to supply a variable to receive the answer in the calling statement. – Print the sum returned)


9. Write a method called underTen which takes the array nums1 as a parameter. It sums all numbers that have a value less than 10 in the array and returns the sum as the value of the method.

10. In the main: add a call to the underTen method, then Print a message telling what the output is followed by the results . For example:
The following is the sum of all numbers in the nums1 array that are less than 10


11. Write a method called smallest which takes the array nums3 as a parameter. It finds the smallest of the number in the array and returns it as the value of the method.

12. In the main: first call the displayArray method so your output looks like this:
The nums3 array after initializing:
10 5 15 (these should be the numbers you assigned in the create and initialize declaration) and so on

Now, add a call to the smallest method passing in nums3 as a parameter, then Print a message telling what the output is followed by the results . For example:
The smallest number in the nums3 array is


13. Write a method called largestIndex which takes the array nums2 as a parameter. It finds the largest of the numbers in the array and returns it’s index as the value of the method.

14. In the main: call the largest method, then Print a message telling what the output is followed by the results . For example:
The index of the largest number in the nums2 array is


15. Write a method called search which takes the array nums3 as a parameter and a random number you want to search for. (just pick one out of your array and pass it in as the second parameter) It finds the FIRST instance of that number and returns its index as the value of the method. If it does not find your random number, it returns a -1

16. In the main: call the search method, then Print a message telling what the output is followed by the results . Use a conditional to print either of these statements: For example:
The random number 6 was found at location 2 OR
The random number 6 was NOT found

17. Write a method called addToOverTwenty which takes the array nums3 as a parameter. It adds 1 to all numbers that have a value greater than 20 in the array . It has no return value.

18. In the main: add a call to the addToOverT wenty method, then Print a message telling what the output is followed by the results of calling the displayArray method. For example:
The nums3 array after adding 1 to all numbers greater than 20 is
10 6 15 and so on (check with the values you assigned to nums3)

19. In the main: Use the Arrays class’s static methods sort and binarySearch to complete the following:
a. Sort nums3 in ascending order
b. Do a binary search on nums3 for a searchNumber you choose
i. Output the number and its index which is returned if found
ii. If not found, binarySearch returns a negative number so output a not found message
c. The Arrays class is in the folder java.util.*; //so add the new import statement!

Posted by rajesh jasani on Wednesday, 04.22.09 @ 22:04pm | #87051

JAVA 1

Objective: To use Swing widgets to develop a GUI
Write an application that presents a colorful ordering screen for Lonny’s Lucky Logos Inc. which sells selected items with team logos to sports arena shops. You will display JButtons for the logos available: Tigers ($2.00), Lions ($3.00), Pistons ($4.00) and Redwings($5.00). The buttons should have icons and text showing the price of each. You will display JCheckBox for the items available: Tee Shirts, Sweat Shirts and Caps. These will be labeled, but do not have to have icons The user may choose only ONE logo, but as many of the items as he wishes to make up a gift set. You will also have a JTextField where the user can enter how many of these sets he wants to order.
When he is finished he will press a done button. You will present a bill as shown in the JOptionPane – see sample screen below. When he has placed his order and seen his bill, he will press a clear button which will erase the Computer bill from the screen so that another user can place an order.






1.Globally declare:
The 6 JButtons, 4 JCheckBoxs and 1 JTextField, any JLabels you need
‘constants’ for the price of each logo as given above (private final double lionlogo=2.0;)
An int variable (numOrdered) to store the number to order entered in the JTextField
A cost (to strore the cost of the logo selected (initialize to 0)
An itemcost to accumulate the cost of all souvenir items ordereds(initialize to 0)
A String initialized to null to accumulate all output
Three Strings initialized to null: capStr, teeStr,sweatStr to store the item selected and to allow deselection
A contentPane
Any ImageIcons desired
private ImageIcon lionlogo = new ImageIcon("lionsp7.gif");
DecimalFormat $###.00 or NumberFormat using US currency
Any font desired
Font f1 = new Font("serif",Font.BOLD,72);
(to apply to any widget: myJLbl.setFont(f1);

2.Write the main
Create an object of this JFrame child, set its size, set its visiblilbty

3.Write the constructor to place all widgets on the screen similar to my example above.
a. Use FlowLayout for the easiest implementation.
b. create and add each widget in the order it should appear on the screen
c. Create an object of a class for handling JButtons (implements ActionListener) and one for handling JCheckBox objects (implements ItemListener) and register components appropriately.

4. in the event handler for JButtons:
Set the global generic cost variable to the selected logo cost based on the button that was clicked and assign to the global string variable an appropriate message and price , like
str = "Pistons @ $ "+df.format(cost); //where mf is an object of the NumberFormat class used for currency formatting
(Use chained if structure since only one of logo can be selected)

If the done button was clicked:
a. Extract the input from the JTextField and convert to integer.
b. Add to the String that is accumulating all your output the sum of the capStr, teeStr, sweatStr (if one or more has been deselected in the execution of the code, it will have a “” (null) value
c. Then add the cost of the logo and itemcost (see JCheckBox info below) and multiply by the number ordered. Output the string and the total cost in a JOptionPane as shown below.














If the clear button is clicked,
a. deslect each JCheckBox: jtb.setSelected(false);
b. set all accumulating variables to 0
c. set all strings to null, and
d. Set the JTextField value to “0”: jtf.setText("0");
e. and finally give the command: repaint(); to clear the screen


5. in the event handler for JCheckBoxs: accumulate values in the global generic variable itemcost

if that JCheckBox is selected, and append an appropriate message to the string, for example
itemcost += capcost;
capStr = "\nCap @ $ "+df.format(capcost);
if that JCheckBox is deselected,
subtract the cost from the itemcost variable and
set the capStr to null
(same logic for all JCheckBoxes)




Posted by rajesh jasani on Wednesday, 04.22.09 @ 22:01pm | #87050

i'm new in java. my prof gave a problem like this. Store 40 characters in an array,such as '1','2','A','B'....Write a program that produces a count of how many characters are vowel letters in the English alphabet. can you help me with this?

Posted by julio on Tuesday, 02.10.09 @ 09:57am | #84700

It is very nice to learn.I am getting understanding easily.I have one doubt in this array session.In the array copy example i can't understand the second system.out line.Please explain it.Another one is the way to copy data you declare in public static code,but here you used in the system.out code.I have confused in that one.please explain it.Thank you.

Posted by sowbakiyavathi on Tuesday, 08.12.08 @ 12:25pm | #72467

array index outof bound---give example and explain

Posted by poornima.s on Monday, 11.26.07 @ 10:42am | #40514

I want a code to display 12 months and select them from a drop down list

Posted by EDWIN on Thursday, 10.18.07 @ 13:49pm | #34325

Ask programming questions?

 

 

Back to Tutorial

 

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name

 

 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.