Total members 11893 |It is currently Tue Nov 05, 2024 12:00 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Creating arrays in java :
Arrays are objects in java ,and for that we create them using new operator .

For primitive data types :
java code
int[] myarr=new int[3];


For not primitive data types :

java code
car[] mycars=new car[3];


Once you specify the size of arrays , you can't change it later .

Assign values to arrays :
java code
myarr[0]=32;
myarr[1]=12;
myarr[2]=50;


for non primitive types :

java code
mycars[0]=new car("BMW");
mycars[1]=new car("VOLVO");
mycars[2]=new car("FIAT");


Create objects in the heap and assign it to the the array elements .

To copy one array to another one :
java code
String[] myStrings = {"one", "two", "three", "four", "five"};
String[] copiedStrings = new String[4];
// 1 is the start position in the orginal position
// 0 is the start position in the copyTo position
System.arraycopy(myStrings, 1, copiedStrings, 0, 4);
for (int i = 0; i < copiedStrings.length; i++) {
System.out.print(copiedStrings[i] + " , ");
}


The output is :
java code
two , three , four , five ,




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

copy array part added.


_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 2 posts ] 

  Related Posts  to : creating arrays in java
 Arrays in java     -  
 Multidimensional arrays in Java     -  
 How to compare two arrays in java     -  
 Comparing Arrays in java     -  
 Creating a JAR file in Java     -  
 Creating your own Exceptions in Java     -  
 Passing arrays as function parameter in java     -  
 creating application for Samsung using java     -  
 Creating a Java Application Build using RAD 7     -  
 Multidimensional Arrays in JSP     -  



Topic Tags

Java Arrays






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com