Вы находитесь на странице: 1из 5

Hi Friend,

Try the following code:

import java.io.*;
import java.util.*;

class Employee{
public int id;
public String name;
public String address;
public static int count = 0;
public Employee(){}
public Employee(int id, String name,String address) {
super();
this.id = id;
this.name = name;
this.address=address;
count++;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
}
public class DisplayArrayList {

public static void main(String[] args) throws Exception {


List<Employee> list = new ArrayList<Employee>();
list.add(new Employee(1, "A","Delhi"));
list.add(new Employee(2, "B","Mumbai"));
list.add(new Employee(3, "C","Chennai"));
list.add(new Employee(4, "D","Kolkata"));
System.out.println(" ");
System.out.print("Enter Employee Id: ");
Scanner input=new Scanner(System.in);
int id = input.nextInt();
for (Employee s : list){
if(id == s.getId())
{
System.out.println("Name and Address of employee is: ");
System.out.print(s.getName()+" " +s.getAddress());

}
}
}
}

----------------------------------------------------------------
String data;
String[][] matrix = new String[4][4];

private void readFile()


{
try
{
Scanner s = new Scanner(this.fname);
String next =null;

for(int i = 0; i < matrix.length; i++)


{
for(int j =0; j < matrix.length; j++)
{
while(s.hasNext() == true)
{

this.matrix[i][j] = next;

}
}
}
}
catch(Exception e)
{
String s = e.toString();
System.out.println(s);
}

}
--------------------------------------------------------------------------
asList(): asList() is a method of Array class that takes an array of any type as
parameter and returns a fixed-size list of the elements of the specified array.

Here is this code of this program:

import java.io.*;
import java.lang.*;
import java.util.*;

public class ArrayListToArray {


public static void main(String args[]) throws IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("How many elements you want to add to the array:");
int n = Integer.parseInt(in.readLine());
String[] name = new String[n];
for(int i = 0; i < n; i++){
name[i] = in.readLine();
}
List<String> list = Arrays.asList(name);
for(String li: list){
String str = li;
System.out.print(str + ",");

}
}
}
-----------------------------------------------------------------------------------
------------
1. read size that you want to define 2D array from file
2. bring it in declare object array size
//this example program read size 2D array from file "dat.txt"

import java.io.*;
class aarr{
public static void main (String[] asg) throws IOException{

FileInputStream f = new FileInputStream("dat.txt");


InputStreamReader input = new InputStreamReader(f,"8859_1");
BufferedReader bufferR = new BufferedReader(input);
String s = bufferR.readLine().trim();
double d = Double.valueOf(s).doubleValue();
bufferR.close();
input.close();
f.close();

int n = (int)d;
double [][] a = new double[n][n];
for(int i = 0;i<n;i++){
for(int j=0;j<n;j++){
a[i][j] = 1.234;
}
System.out.println(i);
}
}
}

-----------------------------------------------------------------------------------
-
Hope this code helps somewhat.
import java.io.*;
public class Tester {

public static void main(String args[])


{
// open a reader over the keyboard (System.in) stream.
BufferedReader reader= new BufferedReader(
new InputStreamReader(System.in));
try {
// make a prompt
System.out.print("Type something: ");
// read one line.
String line=reader.readLine();
// line is the string from the keyboard:
System.out.println("You typed: "+line);
// try to make it a long (the biggest type of integer)
try {
long l = Long.parseLong(line);
System.out.println("It happens to be a whole number: "+l);
} catch (NumberFormatException nfe1) {
// not a whole number.
System.out.println("It is not an whole number");
}
// try to make it a double (the biggest type of float)
try {
double d = Double.parseDouble(line);
System.out.println("It happens to be a floating point number: "+d);
} catch (NumberFormatException nfe2) {
// not a floating point number;
System.out.println("It is not an floating point number (double)");
}
} catch (IOException ioe) {
System.out.println("Something went wrong reading IO:");
ioe.printStackTrace();
}

}
}
----------------------------------------------------------------------
public class MultiplicationTable{
public static void main(String[] args) {
int[][] array = new int[11][11];
for (int i=1; i<array.length; i++) {
for (int j=1; j<array[i].length; j++) {
array[i][j] = i*j;
System.out.print(" " + array[i][j]);
}
System.out.println("");
}
}
----------------------------------------------------------
int[][] a2 = ...;

// Print array even tho we don't know the original size.


for (int r=0; r < a2.length; r++) {
for (int c=0; c < a2[r].length; c++) {
System.out.print(" " + a2[r][c]);
}
System.out.println("");
}
----------------------------------------
//Java program to demonstrate multidimensional arrays
//Topics:
// 1. Simultaneous declaration and initialization
// 2. Use of the length operator to obtain the size
// of multidimensional arrays

public class MultiArray {


// Declare constants
final static int ROWS = 10;

final static int COLS = 5;

public static void main(String[] args) {

// Local varaibles
int rowCount;
int colCount;
int totalSize;

// Declare and allocate an array of bytes


byte[][] screenPix = new byte[ROWS][COLS];

// Obtain and store array dimensions


rowCount = screenPix.length;
colCount = screenPix[COLS].length;
totalSize = rowCount * colCount;

// To obtain the total number of elements of a


// two-dimensional ragged array you need to get the size of
// each array dimension separately

// Display array dimensions


System.out.println("Array row size: " + rowCount);
System.out.println("Array column size: " + colCount);
System.out.println("Total size: " + totalSize);

//*************************
// ragged arrays
//*************************
// First allocate the rows of an array
byte[][] raggedArray = new byte[5][];

// Now allocate the columns


raggedArray[0] = new byte[2];
raggedArray[1] = new byte[2];
raggedArray[2] = new byte[4];
raggedArray[3] = new byte[8];
raggedArray[4] = new byte[3];

// The resulting ragged array is as follows:


// x x
// x x
// x x x x
// x x x x x x x x
// x x x

//************************************
// static array initialization
//************************************
byte[][] smallArray = { { 10, 11, 12, 13 }, { 20, 21, 22, 23 },
{ 30, 31, 32, 33 }, { 40, 41, 42, 43 }, };

// Display the array element at row 2, column 3


System.out.println(smallArray[1][2]); // Value is 21
}
}
----------------------------------------------------------------------------

Вам также может понравиться