Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Slices Allowed?

Status
Not open for further replies.

Kate32

MIS
Joined
Jan 30, 2005
Messages
6
Location
US
Are any kind of slices allowed in Java?
 
Like a slice of pizza ?

[ For those of thus that don't know what a "slice" is ... could you elaborate ]

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
a slice is a substructure of an array - they are used heavily in fortran
 
Do you mean something like :


Array[] array = { 111, 222, 333, 444 };
Array[] slicedArray = array.slice(1, 3);

And 'slicedArray' now holds '222' and '444' ?

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Is that JDK1.5 sedj?

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
no, its just some 'pseudo code' to see if that is what the OP means (in perl you can do something similar).

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
[bigsmile] Thought you'd found something new there, sedj.

Tim
---------------------------
"Your morbid fear of losing,
destroys the lives you're using." - Ozzy
 
Argh, I had to look up "slice" in the dictionary, you prepotent anglosaxons :P

Anyway, I'm not aware of such a feature in Java. I guess you need to do it with a for statement.

For Java people: in Fortran, you have an array and you can tell Fortran to print array values from x to y in a single line array(x:y).

Cheers,
Dian
 
But it wouldn`t be that difficult

Code:
public static Object[] slice(Object[] originalArray, int begin, int end) {
        if (begin<0 || end>originalArray.length)
            throw new SlicedException("Don't slice me!");
	Object[] slicedArray = new Object[length];
	for ( int i=begin; i<end; i++ ) {
		slicedArray[i-begin] = originalArray[i];
	}
	return slicedArray;
	}

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top