Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Help with program

Reply
Thread Tools

Help with program

 
 
Daniel Pitts
Guest
Posts: n/a
 
      05-02-2012
On 5/1/12 6:08 PM, Patricia Shanahan wrote:
> On 5/1/2012 5:06 PM, Daniel Pitts wrote:
> ...
>> static import java.util.Collections.swap;
>> static import java.util.Arrays.asList;
>>
>> (boilerplate code here, that OP must attempt himself)
>> {
>> swap(asList(a), i, j);
>> }
>>

>
> Hmmm. I'm looking forward to the O(n**3) bubble sort.
>
> Patricia


Arrays.asList is actually a O(1) operation, as is Collections.swap, so
it will still only be O(n**2)

Although, none of this works if by "integer array", the OP meant "int[]".


 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      05-02-2012
On 5/1/2012 8:06 PM, Daniel Pitts wrote:
> On 5/1/12 4:51 PM, Arne Vajhøj wrote:
>> On 5/1/2012 4:21 PM, Stefan Ram wrote:
>>> Patricia Shanahan<(E-Mail Removed)> writes:
>>>>> For extra challenge, try doing it without any temporary/local
>>>>> variables.
>>>> I strongly recommend against this, in favor of doing it as simply,
>>>> cleanly and directly as possible.
>>>
>>> What could be more clean and direct than
>>>
>>> java.util.Collections.swap( java.util.Arrays.asList( a ), i, j )
>>>
>>> (assuming »a« has base type Integer[ as given in the OP])?

>>
>> It would be cleaner with an import!
>>
>>


> static import java.util.Collections.swap;
> static import java.util.Arrays.asList;
>
> (boilerplate code here, that OP must attempt himself)
> {
> swap(asList(a), i, j);
> }


I may just have used the traditional import, but ...

Arne


 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      05-02-2012
On 5/2/12 1:25 PM, Arne Vajhøj wrote:
> On 5/1/2012 8:06 PM, Daniel Pitts wrote:
>> On 5/1/12 4:51 PM, Arne Vajhøj wrote:
>>> On 5/1/2012 4:21 PM, Stefan Ram wrote:
>>>> Patricia Shanahan<(E-Mail Removed)> writes:
>>>>>> For extra challenge, try doing it without any temporary/local
>>>>>> variables.
>>>>> I strongly recommend against this, in favor of doing it as simply,
>>>>> cleanly and directly as possible.
>>>>
>>>> What could be more clean and direct than
>>>>
>>>> java.util.Collections.swap( java.util.Arrays.asList( a ), i, j )
>>>>
>>>> (assuming »a« has base type Integer[ as given in the OP])?
>>>
>>> It would be cleaner with an import!
>>>
>>>

>
>> static import java.util.Collections.swap;
>> static import java.util.Arrays.asList;
>>
>> (boilerplate code here, that OP must attempt himself)
>> {
>> swap(asList(a), i, j);
>> }

>
> I may just have used the traditional import, but ...
>
> Arne
>
>

Indeed, I was intentionally obfuscating it so that any decent instructor
would know this was plagiarized.
 
Reply With Quote
 
vishnudhoodhan vishnudhoodhan is offline
Junior Member
Join Date: May 2012
Posts: 3
 
      05-03-2012
Hai hope this will help you


Code:
public class swap {
    
    public static void main(String args[])
    {
        Integer a[] = new Integer[2];
        a[0] = 5;
        a[1] = 10;
        System.out.println("a[0] = " + a[0] + "\t a[1] = " + a[1] );
        swap(a);
        System.out.println("a[0] = " + a[0] + "\t a[1] = " + a[1] );
    }
    
    public static void swap(Integer a[])
    {
        int x;
        x = a[0];
        a[0]= a[1];
        a[1] = x ; //swapped
    }
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help! Can't identify program that resullts in Win 7 on starting to display a message re opening mystery "Program" Seagull NZ Computing 4 12-27-2011 03:46 AM
Looking for a module/program author, or help with program... Kris Stark Perl Misc 1 09-08-2005 01:47 AM
Help !I want to write a program to count the running time of another program freehomesp@yahoo.com.cn C Programming 1 08-12-2005 06:13 AM
Calling Java program in another Java program Rey Java 4 12-12-2003 10:18 PM
passing data between Java program and C program--help pipi Java 1 07-21-2003 05:02 AM



Advertisments