Jump to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Generics - Need Help

Featured Replies

I made a post on stackoverflow but nobody has helped me.

 

I am supposed to write a program that has a generic method to swap any two variables of any data type.

 

I have this, could anyone help me.

public class GArray <E extends Number> {}

private E[] list;

public GArray()

list = (E[]) new Number[10]

I don't know how to write this. Could anyone tell me how to do this?

Thanks

I made a post on stackoverflow but nobody has helped me.

 

I am supposed to write a program that has a generic method to swap any two variables of any data type.

 

I have this, could anyone help me.

public class GArray <E extends Number> {}

private E[] list;

public GArray()

list = (E[]) new Number[10]

I don't know how to write this. Could anyone tell me how to do this?

Thanks

public static <E> List<E> swapIt(List<E> things, E thing1, E thing2) {
      Collections.swap(things, things.indexOf(thing1), things.indexOf(thing2));
      return things;
}

something like this?

  • Author

Thank you for your help. This is my second java course and I can tell you I am not taking up programming anymore. Just taking this class to satisfy needs in Information Technology. How would I go about giving it some sample data like int 10 and string tree?

I am almost done with this course thank god. 1 month

i put this in the main

 

swapIt(things, 10, "tree");
//something is wrong

Edited by Mechagoober

 

Thank you for your help. This is my second java course and I can tell you I am not taking up programming anymore. Just taking this class to satisfy needs in Information Technology. How would I go about giving it some sample data like int 10 and string tree?

I am almost done with this course thank god. 1 month

i put this in the main

 

swapIt(things, 10, "tree");
//something is wrong

 

you're passing two different types. They should both be of 1 type (E)

public static <IIlIiiiIIlII> List<IIlIiiiIIlII> IIIIiiiIIlII (List<IIlIiiiIIlII> IIIIiiiIIlII, IIlIiiiIIlII IIIIiiIIlII, IIlIiiiIIlII IIIIiiIIII) {
     Collections.swap(IIIIiiiIIlII, IIIIiiiIIlII.indexOf(IIIIiiIIlII), IIIIiiiIIlII.indexOf(IIIIiiIIII));
     return IIIIiiiIIlII;
}

just for the sake of it i decided to do this

Edited by Imateamcape

public class Swap {

    public static List<?> swap(List<?> list, Object a, Object b) {
        List<?> result = new ArrayList<>(list);
        Collections.swap(result, list.indexOf(a), list.indexOf(b));
        return result;

    }

    public static <A, B> Object[] swap(Object[] array, Object a, Object b) {
        return swap(Arrays.asList(array), a , b).toArray();
    }

    /*
    Example
    */
    public static void main(String[] args) {
        Object[] array = new Object[]{1, "two", 3, "four"};
        List<?> list = new ArrayList<>(Arrays.asList(array));
        System.out.println(swap(array, 3, "two"));
        System.out.println(swap(list, "four", 1));
    }

}

yw

Edited by Botre

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.