473,385 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Q about java arrays

hello.
in C++, you can fake a sub-array by passing something
like (array + int). If this was passed to something that
takes an array as a parameter, the function will deal
with the array using the int as the starting value.
for example....

if we have some function with the declaration
void someFunctionOnSomeArray(int[] a)

and we have the data array
int[] data = { 1,2,3,4,5,6,7,8,9,0 };

and then we call it with
someFunctionOnSomeArray( data + 5 );

the function will have as the array "a" the values
{ 6,7,8,9,0 }

My Question:
Is there a way to do the same thing in java?

thanks
~johnny

Jul 17 '05 #1
5 8040

"johnny" <sq******@hotmail.com> wrote in message
news:oY*******************@twister.austin.rr.com.. .
hello.
in C++, you can fake a sub-array by passing something
like (array + int). If this was passed to something that
takes an array as a parameter, the function will deal
with the array using the int as the starting value.
for example....

if we have some function with the declaration
void someFunctionOnSomeArray(int[] a)

and we have the data array
int[] data = { 1,2,3,4,5,6,7,8,9,0 };

and then we call it with
someFunctionOnSomeArray( data + 5 );

the function will have as the array "a" the values
{ 6,7,8,9,0 }

My Question:
Is there a way to do the same thing in java?


In C++ an array [single dimension arrays - what may appear to be
multi-dimensional arrays are often fudged using pointers], whether stack or
heap based, is merely a contiguous chunk of memory [even when each element
is, itself, a fully-fledged object]. This, together with the ability to
obtain, and use, the address of arbitrary memory locations, allows the
practice you mention.

In Java an array is an object, and each element of the array is, itself, a
separate entity. Even if each element were contiguously located in memory
[something potentially only useful for primitive-type arrays anyway] there
is no way to obtain the address of arbitrary memory locations. So the answer
is: no.

You would have to rewrite your method [C++ has member data and member
functions, Java has fields and methods] to include a processing start
location:

void someFunctionOnSomeArray(int [] data, int start);

or perhaps specify a range:

void someFunctionOnSomeArray(int [] data, int start, int end);

For better or worse, many of the tricks and shortcuts common in C and C++
code must be abandoned, and a more Java-oriented mindset adopted. This takes
time, and determination.

I hope this helps.

Anthony Borla
Jul 17 '05 #2
Dans l'article <oY*******************@twister.austin.rr.com>, "johnny"
<sq******@hotmail.com> a tapoté :
in C++, you can fake a sub-array by passing something like (array +
int) .... and we have the data array
int[] data = { 1,2,3,4,5,6,7,8,9,0 }; .... the function will have as the array "a" the values { 6,7,8,9,0 } .... My Question:
Is there a way to do the same thing in java?


AFAIK, there's no way to pick a slide from an array, for instance like
that: int[] data = {1,2,3,4,5,6,7} ; getting data[5..data.length-1] is
impossible.

Maybe someone will explain this java drawback.

Anyway, I think it's never a good idea to use primitive types when using
Objects is possible. I've never been confortable about this primitive-object
mix of java. When you read the api, it sometimes look like Sun guys think
everybody have an infinite amount of RAM, but hey, they still use 'int'
as constants instead of static instances. Why ? Saving a few kBytes ?
--
--
A day without sunshine is like a day without orange juice.
----------------------------
jerome.eteve_at_it-omics.com
Jul 17 '05 #3
SMC
On Tue, 02 Dec 2003 20:10:27 +1100, Jéjé wrote:
Dans l'article <oY*******************@twister.austin.rr.com>, "johnny"
<sq******@hotmail.com> a tapoté :
in C++, you can fake a sub-array by passing something like (array +
int)

...
and we have the data array
int[] data = { 1,2,3,4,5,6,7,8,9,0 };

...
the function will have as the array "a" the values { 6,7,8,9,0 }

...
My Question:
Is there a way to do the same thing in java?


AFAIK, there's no way to pick a slide from an array, for instance like
that: int[] data = {1,2,3,4,5,6,7} ; getting data[5..data.length-1] is
impossible.


An array splice (of sorts):

int[] original = { 1, 2, 3, 4, 5 }
int[] splice = new int[3];

System.arraycopy(original, 0, splice, 0, 3);

Would give splice = { 1, 2, 3 }

--
Sean

"There are 10 types of people in this world, those who can count in binary,
and those who can't."
Jul 17 '05 #4
johnny wrote:
hello.
in C++, you can fake a sub-array by passing something like (array +
int). If this was passed to something that takes an array as a
parameter, the function will deal with the array using the int as the
starting value.
for example....

if we have some function with the declaration
void someFunctionOnSomeArray(int[] a)

and we have the data array
int[] data = { 1,2,3,4,5,6,7,8,9,0 };

and then we call it with
someFunctionOnSomeArray( data + 5 );

the function will have as the array "a" the values
{ 6,7,8,9,0 }

My Question:
Is there a way to do the same thing in java?


As many have already indicated, you can't do this with arrays. However,
the java.util.List.subList() method provides a similar functionality.
So, if you are willing to work with Lists, you can use this methodology.
The drawback is that you cannot put primitives in Lists.

Ray

Jul 17 '05 #5
> int[] original = { 1, 2, 3, 4, 5 }
int[] splice = new int[3];

System.arraycopy(original, 0, splice, 0, 3);

Would give splice = { 1, 2, 3 }


Good old C style :)

--
--
Agree with them now, it will save so much time.
----------------------------
jerome.eteve_at_it-omics.com
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

73
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
15
by: Paul Morrison | last post by:
Hi all, I need to come up with some differences between arrays in Java and C, I have searched Google and so far all I have found is the following: Arrays in Java are reference types with...
10
by: Ole | last post by:
Goodday everybody, i want to create an array that represents data, that has to be transferred to a pdf doc ( iTextSharp ). The problem is, that i seem too loose my faith. Namely, ( i have...
2
by: Krzysztof Paz | last post by:
Hi, There is a Java (SUN 1.4) server which using Object Input/Output Streams at SSL/Socket to communicate with Java clients. Now there is a request for making C# Client for this server also. SSL...
458
by: wellstone9912 | last post by:
Java programmers seem to always be whining about how confusing and overly complex C++ appears to them. I would like to introduce an explanation for this. Is it possible that Java programmers...
26
by: Christoph Zwerschke | last post by:
You will often hear that for reasons of fault minimization, you should use a programming language with strict typing: http://turing.une.edu.au/~comp284/Lectures/Lecture_18/lecture/node1.html I...
2
by: rookiejavadude | last post by:
I'm have most of my java script done but can not figure out how to add a few buttons. I need to add a delete and add buttong to my existing java program. Not sure were to add it on how. Can anyone...
0
by: r035198x | last post by:
Inheritance We have already covered one important concept of object-oriented programming, namely encapsulation, in the previous article. These articles are not articles on object oriented...
7
by: Sanny | last post by:
I have an app in Java. It works fine. Some people say Java works as fast as C. Is that true? C can use assembly language programs. How much faster are they inplace of calling general routines. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.