DreamsCoder.com

We Code your Dreams
home > java programs

Difference between ArrayList and LinkedList in Java with example





Difference between ArrayList and LinkedList in Java with example

/* LINKEDLIST EXAMPLE */
	LinkedList ll = new LinkedList();
  
      ll.add("Dreams");
      ll.add("Coder");
      ll.add(".com");
      ll.addLast("Programming");
      ll.addFirst("www.");
      System.out.println("Original contents of ll:"+ll);

      // remove elements from the linked list
      ll.remove(".com");
      ll.remove(1);
      System.out.println("Contents of Linked List 
ll after deletion: "+ ll);
      
      // remove first and last elements
      ll.removeFirst();
      ll.removeLast();
      System.out.println("Contents of Linked List 
ll after deleting first and last: "+ ll);

/* ARRAYLIST EXAMPLE */
     ArrayList arrlist = new
 ArrayList(5);

   arrylist.add(1);
   arrylist.add(2);
   arrylist.add(3);
   arrylist.add(4);

   for (Integer num : arrylist) {
   System.out.println("Number = " + number);
   } 
	
   // retrieves element at 3rd postion
   int third =arrylist.get(2);
   System.out.println("Retrieved element is = " +third);
}

Label - java





AES Encryption and Decryption using Java





AES Encryption and Decryption using Java


import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class AES {

    public void operation() {
      try {
         String text = "Hello DreamsCoder reader";
         
        String key = "x4690alo09549a12"; // 128 bit key
         
         // Create key and cipher
         Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
         Cipher cipher = Cipher.getInstance("AES");

         // encrypt the text
         cipher.init(Cipher.ENCRYPT_MODE, aesKey);
         byte[] encrypted = 
cipher.doFinal(text.getBytes());
         System.out.println("Encrypted Text	");
         System.err.print(new String(encrypted));
  
         // decrypt the text
         cipher.init(Cipher.DECRYPT_MODE, aesKey);
         String decrypted = new
String(cipher.doFinal(encrypted));
         System.out.println("Decrypted Text	");
         System.err.print(decrypted);
      }catch(Exception e) {
         e.printStackTrace();
      }
    }

    public static void main(String[] args) {
       AES app = new AES();
       app.operation();
    }
}

Label - java





Dynamically Updating a List in JAVA





Dynamically Updating a List in JAVA

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

class DynListExample
		extends 	JFrame
		implements	ActionListener,
					ListSelectionListener
 {
// Instance attributes used in this example
	private	JPanel		topPanel;
	private	JList		listbox;
	private	Vector		listData;
	private	JButton		addButton;
	private	JButton		removeButton;
	private	JTextField	dataField;
	private	JScrollPane scrollPane;


	// Constructor of main frame
	public DynListExample()
	{
// Set the frame characteristics
		setTitle( "Advanced List Box Application" );
		setSize( 300, 100 );
		setBackground( Color.gray );

// Create a panel to hold all other components
		topPanel = new JPanel();
		topPanel.setLayout( new BorderLayout() );
		getContentPane().add( topPanel );

// Create the data model for this example
		listData = new Vector();

// Create a new listbox control
		listbox = new JList( listData );
		listbox.
addListSelectionListener( this );

// Add the listbox to a scrolling pane
		scrollPane = new JScrollPane();
scrollPane.getViewport().add( listbox );
		topPanel.add
( scrollPane, BorderLayout.CENTER );

		CreateDataEntryPanel();
	}


	public void CreateDataEntryPanel()
	{
// Create a panel to hold all other components
		JPanel dataPanel = new JPanel();
		dataPanel.setLayout
( new BorderLayout() );
		topPanel.add( dataPanel, BorderLayout.SOUTH );

		// Create some function buttons
		addButton = new JButton( "Add" );
		dataPanel.add
( addButton, BorderLayout.WEST );
	addButton.addActionListener( this );

removeButton = new JButton( "Delete" );
dataPanel.add( removeButton, BorderLayout.EAST );
removeButton.addActionListener( this );

// Create a text field for data entry and display
	dataField = new JTextField();
	dataPanel.add
( dataField, BorderLayout.CENTER );
	}

// Handler for list selection changes
 public void valueChanged( 
ListSelectionEvent event )
 	{
 // See if this is a listbox selection and the
// event stream has settled
if( event.getSource() == listbox						

&& !event.getValueIsAdjusting() )
		{
// Get the current selection and place it in the		// edit field
			String stringValue = (String)listbox.getSelectedValue();
	if( stringValue != null )		dataField.setText( stringValue );
		}
 	}

// Handler for button presses
public void actionPerformed( ActionEvent event )
	{
	if( event.getSource() == addButton )
	{
// Get the text field value			String stringValue = dataField.getText();
			dataField.setText( "" );

	// Add this item to the list and refresh
			if( stringValue != null )
			{
listData.addElement( stringValue );
	listbox.setListData( listData );
				scrollPane.revalidate();
				scrollPane.repaint();
			}
		}

	if( event.getSource() == removeButton )
		{
	// Get the current selection
	int selection = 
listbox.getSelectedIndex();
			if( selection >= 0 )
			{
// Add this item to the list and refresh
				listData.removeElementAt( selection );
listbox.setListData( listData );
				scrollPane.revalidate();
				scrollPane.repaint();

// As a nice touch, select the next item
			if( selection 
>= listData.size() )
	selection = listData.size() - 1;
				listbox.setSelectedIndex( selection );
			}
		}
	}

	// Main entry point for this example
	public static void main( String args[] )
	{
// Create an instance of the test application
DynListExample mainFrame =
 new DynListExample();
		mainFrame.setVisible( true );
	}
}

Label - java





Upload and Preview Image using JFrame in Java





Upload and Preview Image using JFrame in Java

import java.awt.*; 
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.swing.*;
import java.io.* ;


class Accept implements ActionListener
{
	String ImageName ;
	JFrame f ;
	JFileChooser file ;
	JButton Browse , Ok ;
	JTextField a ;
	
	public Accept()
	{
		f = new JFrame("Select Image") ;
		f.setBounds(400, 200, 400, 100) ;
		f.setLayout(new FlowLayout()) ;
		Browse = new JButton("Browse") ;
		Browse.addActionListener(this) ;
		Ok = new JButton("Show Image") ;
		Ok.addActionListener(this) ;
		a = new JTextField(20) ;
		f.add(a) ;
		f.add(Browse) ;
		f.add(Ok) ;
		f.setVisible(true) ;
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==Ok)
		{
			if(a.getText().equals(""))
			{
				JOptionPane.showMessageDialog
(f,"Please Select an Image","Error",JOptionPane.ERROR_MESSAGE) ;
			}
			else
			{
				 
             ImageName = a.getText() ;
	       new GraphicsSetA4(ImageName) ;
			}
		}
		else if(e.getSource()==Browse)
		{
			file = new JFileChooser() ;
			file.showOpenDialog(f) ;
			
String s = file.getSelectedFile().getAbsolutePath() ;
			a.setText(s) ;
		}
	}
}

public class GraphicsSetA4 extends JFrame 
{
	Font f ;
	BufferedImage image ;
	File file ;
	
	public GraphicsSetA4(String Image)
	{
		super("Frame") ;
		file = new File(Image) ;
		try
		{
			image = ImageIO.read(file) ;
		}
		catch(IOException e)
		{
			System.out.println(e) ; 
		}
		setBounds(400,200,430,400) ;
		setVisible(true) ;
		setDefaultCloseOperation
(EXIT_ON_CLOSE) ;
	}
	
	public void paint(Graphics g)
	{
		g.drawImage(image, 0, 0, null) ;
	}
	
	public static void main(String[] arg)
	{
		new Accept() ;
	}
}

Label - java





Connect Database and Retrieve values using JSP



<%@ page import="java.sql.*" %>

<% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 

        
        <% 
            Connection connection =
DriverManager.getConnection(
                "jdbc:odbc:data", "userName",
"password");

            Statement statement =
connection.createStatement() ;
            ResultSet resultset =
statement.executeQuery("select * from Publishers") ; 
        %>

    

        
            <% while(resultset.next()){ %>
            
            <% } %>
        
ID Name City State Country
<%= resultset.getString(1) %> <%= resultset.getString(2) %> <%= resultset.getString(3) %> <%= resultset.getString(4) %> <%= resultset.getString(5) %>

Label - java





next page >






Privacy Policy
Copyright © 2018 by DreamsCoder. All Rights Reserved.
DreamsCoder Google Plus DreamsCoder Facebook



Latest Technology,Tricks and Tips