Skip to content

Create Array List.java #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
208 changes: 208 additions & 0 deletions Array List.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
public class MyArrayList<AnyType> implements Iterable<AnyType>
{
/**
* Construct an empty ArrayList.
*/
public MyArrayList( )
{
doClear( );
}

/**
* Returns the number of items in this collection.
* @return the number of items in this collection.
*/
public int size( )
{
return theSize;
}

/**
* Returns true if this collection is empty.
* @return true if this collection is empty.
*/
public boolean isEmpty( )
{
return size( ) == 0;
}

/**
* Returns the item at position idx.
* @param idx the index to search in.
* @throws ArrayIndexOutOfBoundsException if index is out of range.
*/
public AnyType get( int idx )
{
if( idx < 0 || idx >= size( ) )
throw new ArrayIndexOutOfBoundsException( "Index " + idx + "; size " + size( ) );
return theItems[ idx ];
}

/**
* Changes the item at position idx.
* @param idx the index to change.
* @param newVal the new value.
* @return the old value.
* @throws ArrayIndexOutOfBoundsException if index is out of range.
*/
public AnyType set( int idx, AnyType newVal )
{
if( idx < 0 || idx >= size( ) )
throw new ArrayIndexOutOfBoundsException( "Index " + idx + "; size " + size( ) );
AnyType old = theItems[ idx ];
theItems[ idx ] = newVal;

return old;
}

@SuppressWarnings("unchecked")
public void ensureCapacity( int newCapacity )
{
if( newCapacity < theSize )
return;

AnyType [ ] old = theItems;
theItems = (AnyType []) new Object[ newCapacity ];
for( int i = 0; i < size( ); i++ )
theItems[ i ] = old[ i ];
}

/**
* Adds an item to this collection, at the end.
* @param x any object.
* @return true.
*/
public boolean add( AnyType x )
{
add( size( ), x );
return true;
}

/**
* Adds an item to this collection, at the specified index.
* @param x any object.
* @return true.
*/
public void add( int idx, AnyType x )
{
if( theItems.length == size( ) )
ensureCapacity( size( ) * 2 + 1 );

for( int i = theSize; i > idx; i-- )
theItems[ i ] = theItems[ i - 1 ];

theItems[ idx ] = x;
theSize++;
}

/**
* Removes an item from this collection.
* @param idx the index of the object.
* @return the item was removed from the collection.
*/
public AnyType remove( int idx )
{
AnyType removedItem = theItems[ idx ];

for( int i = idx; i < size( ) - 1; i++ )
theItems[ i ] = theItems[ i + 1 ];
theSize--;

return removedItem;
}

/**
* Change the size of this collection to zero.
*/
public void clear( )
{
doClear( );
}

private void doClear( )
{
theSize = 0;
ensureCapacity( DEFAULT_CAPACITY );
}

/**
* Obtains an Iterator object used to traverse the collection.
* @return an iterator positioned prior to the first element.
*/
public java.util.Iterator<AnyType> iterator( )
{
return new ArrayListIterator( );
}

/**
* Returns a String representation of this collection.
*/
public String toString( )
{
StringBuilder sb = new StringBuilder( "[ " );

for( AnyType x : this )
sb.append( x + " " );
sb.append( "]" );

return new String( sb );
}

/**
* This is the implementation of the ArrayListIterator.
* It maintains a notion of a current position and of
* course the implicit reference to the MyArrayList.
*/
private class ArrayListIterator implements java.util.Iterator<AnyType>
{
private int current = 0;
private boolean okToRemove = false;

public boolean hasNext( )
{
return current < size( );
}


public AnyType next( )
{
if( !hasNext( ) )
throw new java.util.NoSuchElementException( );

okToRemove = true;
return theItems[ current++ ];
}

public void remove( )
{
if( !okToRemove )
throw new IllegalStateException( );

MyArrayList.this.remove( --current );
okToRemove = false;
}
}

private static final int DEFAULT_CAPACITY = 10;

private AnyType [ ] theItems;
private int theSize;
}

class TestArrayList
{
public static void main( String [ ] args )
{
MyArrayList<Integer> lst = new MyArrayList<>( );

for( int i = 0; i < 10; i++ )
lst.add( i );
for( int i = 20; i < 30; i++ )
lst.add( 0, i );

lst.remove( 0 );
lst.remove( lst.size( ) - 1 );

System.out.println( lst );
}
}
64 changes: 64 additions & 0 deletions image processing.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Java program to demonstrate
// colored to grayscale conversion

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Grayscale {
public static void main(String args[])
throws IOException
{
BufferedImage img = null;
File f = null;

// read image
try {
f = new File(
"C:/Users/hp/Desktop/Image Processing in Java/gfg-logo.png");
img = ImageIO.read(f);
}
catch (IOException e) {
System.out.println(e);
}

// get image's width and height
int width = img.getWidth();
int height = img.getHeight();

// convert to grayscale
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {

// Here (x,y)denotes the coordinate of image
// for modifying the pixel value.
int p = img.getRGB(x, y);

int a = (p >> 24) & 0xff;
int r = (p >> 16) & 0xff;
int g = (p >> 8) & 0xff;
int b = p & 0xff;

// calculate average
int avg = (r + g + b) / 3;

// replace RGB value with avg
p = (a << 24) | (avg << 16) | (avg << 8)
| avg;

img.setRGB(x, y, p);
}
}

// write image
try {
f = new File(
"C:/Users/hp/Desktop/Image Processing in Java/GFG.png");
ImageIO.write(img, "png", f);
}
catch (IOException e) {
System.out.println(e);
}
}
}